PR114 Sliding-Window Pulse Counting Without Arrays

Jason IP2 min read
Other ManufacturerOther TopicPLC Programming
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

Define the Sliding-Window Requirement

The PR114 must count selected input edges received during the interval immediately preceding the current time.

Design item Evidence-controlled value Engineering consequence
Input event Rising or falling edge Select one edge and count it once after filtering.

Select a History Representation

A true sliding window requires historical state: the controller must add new pulses and remove pulses that become older than the window. With bucket width Δt and window W, the required bucket count is N = W/Δt when W is divisible by Δt. At the maximum 180-second window, 10-second buckets require 18 stored counts; 1-second buckets require 180.

A bit-level shift register sampled at the maximum pulse rate would require 180 × 11 = 1,980 bits, but this approach assumes regular sampling and suitable shift-register support. Bucketed counts are more compact because each bucket stores the number of accepted edges during Δt. The evidence conflicts on whether PR114 provides array-like storage, so confirm the available data objects in the applicable programming documentation before selecting an implementation.

Implement Bucketed Counting

If arrays or indexed history are unavailable, implement N fixed bucket registers and rotate their roles with a step index or equivalent fixed logic. The running total avoids summing every bucket on every update.

  1. Count each accepted rising or falling edge in the active bucket.
  2. Every Δt, advance to the bucket that is leaving the window.
  3. Subtract that bucket from the running total, clear it, and make it the new active bucket.
  4. Add each subsequently accepted edge to both the active bucket and the running total.
  5. Expose the running total as the pulse count for the current window.
OnAcceptedEdge:
    ActiveBucket := ActiveBucket + 1
    WindowTotal  := WindowTotal + 1

OnBucketBoundary:
    Advance to next fixed bucket
    WindowTotal := WindowTotal - NextBucket
    NextBucket  := 0
    ActiveBucket := NextBucket

If the configured window is not an integer multiple of Δt, bucket rotation cannot represent it exactly. Select Δt so W/Δt is integral, or document the resulting quantization rather than silently changing the window.

Filter and Verify the Input

The evidence does not establish the reed-switch bounce duration or confirm which PR114 input-filter setting is available. Do not assign an unsupported debounce time. First verify the applicable discrete-input configuration in the PR114 documentation. If an input filter is available, set it from measured bounce behavior; otherwise, qualify the edge in logic before incrementing the bucket. The cited DTRIG approach is not sufficiently defined to treat it as a proven debounce method.

Verify the program with a known pulse source. Confirm that one physical actuation produces one accepted edge, the total increases once per accepted edge, a bucket's contribution disappears after W seconds within the selected Δt resolution, and the count remains correct at rates up to the stated 11 Hz. Also confirm that the chosen counter and bucket data types can represent at least the calculated 1,980-pulse maximum for a 180-second window at 11 Hz.

FAQ

How many buckets does a 180-second sliding window need?

It needs 18 buckets at 10-second resolution or 180 buckets at 1-second resolution, using N = W/Δt.

What is the maximum pulse count in a 180-second window at 11 Hz?

The calculated maximum is 11 × 180 = 1,980 pulses. Select bucket and total data types that can represent at least this value.

Can DTRIG remove reed-switch contact bounce on a PR114?

The evidence does not establish that DTRIG alone performs debounce or provide the required delay. Measure the bounce, verify whether the applicable PR114 discrete input has configurable filtering, and confirm that one actuation produces one accepted edge.

Back to blog