Step sequence with timers in PLC Programming - Time & Space Complexity
When working with step sequences and timers in PLC programming, it's important to know how the program's execution time changes as the number of steps grows.
We want to understand how the time to complete all steps changes when we add more steps with timers.
Analyze the time complexity of the following PLC step sequence with timers.
// Step sequence with timers
FOR step := 1 TO totalSteps DO
StartTimer(stepTimer[step], presetTime[step]);
WHILE NOT TimerDone(stepTimer[step]) DO
; // wait for timer
END_WHILE;
ExecuteStepAction(step);
END_FOR;
This code runs through each step, starts a timer, waits until the timer finishes, then executes the step action.
Look at what repeats in this code:
- Primary operation: The FOR loop runs through each step.
- How many times: It runs once for every step, so totalSteps times.
- Inside the loop, the program waits for the timer to finish before moving on.
As the number of steps increases, the total time grows because the program waits for each timer in order.
| Input Size (totalSteps) | Approx. Operations (waiting + actions) |
|---|---|
| 10 | Waits for 10 timers, then 10 actions |
| 100 | Waits for 100 timers, then 100 actions |
| 1000 | Waits for 1000 timers, then 1000 actions |
Pattern observation: The total execution time grows directly with the number of steps because each timer must finish before the next starts.
Time Complexity: O(n)
This means the total time grows linearly with the number of steps in the sequence.
[X] Wrong: "The timers run at the same time, so total time stays the same no matter how many steps."
[OK] Correct: In this code, timers run one after another, not in parallel, so total time adds up for each step.
Understanding how step sequences with timers affect execution time helps you design efficient control programs and explain your reasoning clearly in interviews.
What if we changed the code to start all timers at once and then wait for all to finish? How would the time complexity change?
Practice
Solution
Step 1: Understand the role of timers in step sequences
Timers are used to create delays between steps, ensuring each step runs for a set time.Step 2: Identify the correct timer function
The timer waits before allowing the sequence to move to the next step, controlling timing precisely.Final Answer:
To wait a set time before moving to the next step -> Option AQuick Check:
Timers delay steps = A [OK]
- Thinking timers skip steps
- Assuming timers reset the program
- Believing timers jump to last step
Solution
Step 1: Recognize standard PLC timer syntax
In many PLC languages, the TON (Timer ON Delay) instruction is used with a timer name and preset time.Step 2: Compare options to standard syntax
TON T1, 5s uses 'TON T1, 5s' which matches common PLC timer start syntax.Final Answer:
TON T1, 5s -> Option CQuick Check:
TON is standard timer start = C [OK]
- Using incorrect function names
- Missing TON keyword
- Wrong timer parameter format
Step 1: Start TON timer T1 for 3 seconds
Step 2: When T1 done, move to Step 2 and start TON timer T2 for 2 seconds
Step 3: When T2 done, move to Step 3
What is the total time before Step 3 starts?
Solution
Step 1: Add timer durations sequentially
Step 1 waits 3 seconds, then Step 2 waits 2 seconds before Step 3.Step 2: Calculate total wait time
Total time = 3 seconds + 2 seconds = 5 seconds.Final Answer:
5 seconds -> Option AQuick Check:
3s + 2s = 5s total [OK]
- Only counting one timer duration
- Confusing timer start with timer done
- Assuming timers run in parallel
Solution
Step 1: Identify why timer causes no delay
If the timer preset is zero, it finishes instantly, causing no wait.Step 2: Check other options
Counting up correctly means timer works; missing sequence or power off would stop all operation.Final Answer:
Timer preset value is set to zero -> Option BQuick Check:
Zero preset = no delay [OK]
- Ignoring timer preset value
- Assuming power off causes immediate step
- Confusing timer counting with preset
1. Starts motor for 4 seconds,
2. Waits 3 seconds with motor off,
3. Then starts motor again.
Which timer setup correctly implements this sequence?
Solution
Step 1: Match motor state with timers per step
Step 1 must run motor ON while timer T1 counts 4 seconds; Step 2 motor OFF with timer T2 for 3 seconds; Step 3 motor ON again.Step 2: Verify options
Only Step 1: Motor ON and TON T1, 4s; Step 2: Motor OFF and TON T2, 3s; Step 3: Motor ON correctly pairs motor ON/OFF states with timers in the right steps.Final Answer:
Step 1: Motor ON and TON T1, 4s; Step 2: Motor OFF and TON T2, 3s; Step 3: Motor ON -> Option DQuick Check:
Motor state matches timers per step = D [OK]
- Not turning motor OFF during wait
- Mixing timer durations
- Skipping motor control in steps
Ready to prove it?
Pass the test and this pattern moves unknown
