Counter preset and reset in PLC Programming - Time & Space Complexity
When using counters with preset and reset in PLC programming, it's important to see how the program's steps grow as input changes.
We want to know how the time to update the counter changes when the count value or inputs change.
Analyze the time complexity of the following code snippet.
// Simple counter with preset and reset
IF input_signal THEN
counter_value := counter_value + 1;
END_IF;
IF counter_value >= preset_value THEN
output_signal := TRUE;
END_IF;
IF reset_signal THEN
counter_value := 0;
END_IF;
This code increases a counter when an input is true, checks if it reached a preset, and resets it when a reset signal is true.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking input signals and updating the counter once per scan cycle.
- How many times: This happens once every program scan, not inside a loop.
The program checks and updates the counter once each scan, regardless of the preset value size.
| Input Size (preset_value) | Approx. Operations per Scan |
|---|---|
| 10 | 3 (check input, increment, compare) |
| 100 | 3 (same steps, no extra work) |
| 1000 | 3 (still same steps) |
Pattern observation: The number of operations stays the same no matter how big the preset is.
Time Complexity: O(1)
This means the time to update and check the counter does not grow with the preset value size; it stays constant.
[X] Wrong: "The bigger the preset value, the longer the counter takes to update."
[OK] Correct: The counter update and comparison happen once per scan, no matter the preset size, so time does not increase with preset.
Understanding how simple operations like counters behave helps you explain efficient control logic in automation tasks.
"What if the counter was inside a loop that runs up to the preset value? How would the time complexity change?"
Practice
preset value in a PLC counter do?Solution
Step 1: Understand the role of preset in counters
The preset value defines the goal number the counter counts up to before triggering an action.Step 2: Differentiate preset from reset and increment
Reset clears the count, increment adds to count, but preset sets the target number.Final Answer:
It sets the target count the counter must reach. -> Option AQuick Check:
Preset = target count [OK]
- Confusing preset with reset
- Thinking preset increments count
- Assuming preset pauses counting
CNT1 in ladder logic?Solution
Step 1: Identify reset instruction syntax
In PLC ladder logic,RESis the standard instruction to reset a counter.Step 2: Confirm other instructions meanings
PREsets preset,INCincrements,SETsets bits, not reset counter.Final Answer:
UseRES CNT1instruction. -> Option AQuick Check:
Reset = RES instruction [OK]
- Using PRE instead of RES to reset
- Confusing increment with reset
- Using SET which is for bits, not counters
Solution
Step 1: Calculate count after increments
Starting count is 3, two increments add 2, so 3 + 2 = 5.Step 2: Compare count to preset
The count reaches the preset value 5 after two increments.Final Answer:
5 -> Option BQuick Check:
3 + 2 = 5 [OK]
- Not adding increments correctly
- Confusing preset with current count
- Assuming count resets automatically
Solution
Step 1: Check reset wiring and instruction
If the reset button does not trigger reset, likely the reset instruction is missing or not linked to the button.Step 2: Eliminate other causes
Preset value or current count does not prevent reset; increment instruction wrong but would not cause no reset on button press.Final Answer:
The reset instruction is missing or not connected to the reset button. -> Option DQuick Check:
Reset fails if instruction missing or unlinked [OK]
- Blaming preset for reset failure
- Ignoring wiring or logic connection
- Confusing increment with reset
Solution
Step 1: Understand automatic reset logic
To reset automatically, use the counter's done bit (which is true when count reaches preset) to trigger the reset instruction.Step 2: Evaluate other options
Manual reset requires user action, preset 0 is invalid for counting, resetting only before counting won't loop.Final Answer:
Set preset to 10, use counter done bit to trigger reset instruction. -> Option CQuick Check:
Done bit triggers reset for auto count loop [OK]
- Relying on manual reset for auto counting
- Setting preset to zero
- Resetting only once before counting
Ready to prove it?
Pass the test and this pattern moves unknown
