Cascading timers for long delays in PLC Programming - Time & Space Complexity
When using cascading timers in PLC programming, it is important to understand how the execution time grows as the delay length increases.
We want to know how the number of timer checks or steps changes when we set longer delays.
Analyze the time complexity of the following cascading timers code snippet.
// Initialize timers
Timer1.Preset := 1000; // 1 second
Timer2.Preset := 1000; // 1 second
// Start Timer1
IF NOT Timer1.Done THEN
Timer1.Start := TRUE;
ELSE
Timer2.Start := TRUE;
END_IF
This code uses two timers in sequence to create a longer delay by starting Timer2 only after Timer1 finishes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking timer completion status repeatedly.
- How many times: Timer1 runs for its preset time, then Timer2 runs for its preset time.
As the total delay increases by adding more timers or longer presets, the total checks grow proportionally.
| Input Size (total delay in seconds) | Approx. Operations (timer checks) |
|---|---|
| 2 (2 timers x 1s) | About 2000 checks |
| 10 (10 timers x 1s) | About 10,000 checks |
| 100 (100 timers x 1s) | About 100,000 checks |
Pattern observation: The number of timer checks grows linearly with the total delay length.
Time Complexity: O(n)
This means the time to complete the delay grows directly in proportion to the total delay length set by cascading timers.
[X] Wrong: "Using more timers in cascade does not affect execution time much because each timer runs independently."
[OK] Correct: Each timer must complete before the next starts, so total execution time adds up linearly with each timer's delay.
Understanding how cascading timers affect execution time helps you design efficient control systems and explain timing behavior clearly in real projects.
"What if we replaced cascading timers with a single timer set to the total delay? How would the time complexity change?"
Practice
Solution
Step 1: Understand the limitation of single timers
Single timers have a maximum delay limit, so they cannot handle very long delays alone.Step 2: Explain cascading timers concept
Cascading timers link several short timers in sequence, where each starts after the previous finishes, to create a longer total delay.Final Answer:
To combine multiple short timers to achieve a longer delay -> Option AQuick Check:
Cascading timers extend delay by chaining short timers [OK]
- Assuming timers run at the same time
- Believing timers reset automatically
- Thinking cascading reduces timer count
Solution
Step 1: Identify the trigger for the second timer
The second timer should start only after the first timer finishes, indicated by its done bit being ON.Step 2: Eliminate incorrect options
Starting the second timer while the first is running or immediately after it starts is incorrect because it breaks the cascade logic.Final Answer:
Start the second timer when the first timer's done bit is ON -> Option BQuick Check:
Second timer triggers on first timer done bit [OK]
- Starting second timer too early
- Using preset value instead of done bit
- Starting timers simultaneously
Timer1: PT=10s, DN bit set after 10s
Timer2: PT=20s, starts when Timer1.DN is ON
Timer3: PT=30s, starts when Timer2.DN is ON
What is the total delay before Timer3.DN becomes ON?
Solution
Step 1: Calculate each timer's delay
Timer1 delays 10 seconds, Timer2 delays 20 seconds, Timer3 delays 30 seconds.Step 2: Add delays in cascade
Total delay = 10s + 20s + 30s = 60 seconds.Final Answer:
60 seconds -> Option DQuick Check:
Sum of all timer delays = 60 seconds [OK]
- Taking only last timer delay
- Multiplying delays instead of adding
- Confusing seconds with minutes
Solution
Step 1: Analyze the start condition for the second timer
The second timer must start when the first timer's done bit is ON; if not, it won't start.Step 2: Identify the error
If the done bit is not used as the start condition, the second timer never triggers, causing the issue.Final Answer:
The first timer's done bit is not used as a start condition for the second timer -> Option AQuick Check:
Second timer needs first timer done bit to start [OK]
- Confusing running bit with done bit
- Blaming preset times for start issues
- Ignoring start condition linkage
Solution
Step 1: Understand timer preset limit
The timer cannot be set above 1 minute, so a single timer cannot create a 5-minute delay.Step 2: Apply cascading timers to extend delay
Use 5 timers of 1 minute each, starting each next timer when the previous timer's done bit is ON, to sum up to 5 minutes.Final Answer:
Use 5 timers in sequence, each set to 1 minute, starting the next when the previous timer's done bit is ON -> Option CQuick Check:
Cascade multiple 1-minute timers to reach 5 minutes [OK]
- Trying to set timer above max preset
- Starting timers simultaneously instead of cascading
- Looping single timer without done bit control
Ready to prove it?
Pass the test and this pattern moves unknown
