Flashing light circuit with timers in PLC Programming - Time & Space Complexity
We want to understand how the time it takes for a flashing light circuit to run changes as we adjust its timers.
Specifically, we ask: how does the program's execution time grow when timers are involved?
Analyze the time complexity of the following code snippet.
// Simple flashing light with timer
TON Timer1;
Timer1.PT := T#1s; // Set timer to 1 second
Timer1.IN := TRUE; // Start timer
IF Timer1.Q THEN
Light := NOT Light; // Toggle light
Timer1.IN := FALSE; // Reset timer
END_IF;
This code toggles a light on and off every second using a timer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The timer check and light toggle happen repeatedly each scan cycle.
- How many times: This runs continuously, once every scan cycle of the PLC program.
The program runs the same steps every scan, regardless of timer length.
| Input Size (Timer Duration) | Approx. Operations per Scan |
|---|---|
| 1 second | Constant number of operations |
| 10 seconds | Constant number of operations |
| 100 seconds | Constant number of operations |
Pattern observation: The number of operations per scan does not grow with timer duration; it stays the same.
Time Complexity: O(1)
This means the program's execution time stays constant no matter how long the timer is set.
[X] Wrong: "Longer timer durations make the program take longer to run."
[OK] Correct: The timer runs independently and the program only checks its status each scan, so execution time stays the same.
Understanding how timers affect program flow and execution time is a key skill in automation and control programming.
What if we added a loop that toggles multiple lights based on an array of timers? How would the time complexity change?
Practice
Solution
Step 1: Understand timer function in PLC
Timers in PLCs are used to create delays or measure time intervals before an action occurs.Step 2: Apply timer role in flashing light circuit
In a flashing light circuit, the timer delays turning the light on or off to create the flashing effect.Final Answer:
To create a delay that controls when the light turns on or off -> Option CQuick Check:
Timer delays control flashing = B [OK]
- Thinking timers increase brightness
- Confusing timers with voltage measurement
- Assuming timers keep lights permanently on
Solution
Step 1: Recall standard TON timer syntax in PLC
The TON timer uses named parameters with := and time format T# for preset time.Step 2: Match syntax to options
TON T1(IN:=Start, PT:=T#5s); uses correct parameter names and time format: TON T1(IN:=Start, PT:=T#5s); others are incorrect or incomplete.Final Answer:
TON T1(IN:=Start, PT:=T#5s); -> Option DQuick Check:
Correct TON syntax uses := and T# time format = D [OK]
- Using parentheses without := for parameters
- Writing time as plain number without T#
- Incorrect keyword order or missing parameters
TON T1(IN:=Start, PT:=T#2s); IF T1.Q THEN Light := NOT Light; T1(IN:=FALSE); END_IF;
What happens to the Light output after 2 seconds of Start being TRUE?
Solution
Step 1: Analyze timer and toggle logic
The timer T1 starts when Start is TRUE. After 2 seconds, T1.Q becomes TRUE, toggling Light once.Step 2: Check timer reset behavior
After toggling, T1 is set to FALSE (stopped), so no further toggling occurs.Final Answer:
Light toggles once then stops -> Option BQuick Check:
Timer reset stops repeated toggling = A [OK]
- Assuming continuous toggling without timer reset
- Thinking light stays ON permanently
- Ignoring timer reset effect
TON T1(IN:=Start, PT:=T#3s); IF T1.Q THEN Light := NOT Light; END_IF; T1(IN:=Start);
Solution
Step 1: Check order of timer input assignment
The timer input IN must be assigned before checking T1.Q to update timer status correctly.Step 2: Identify impact of wrong order
Assigning IN after checking T1.Q means T1.Q is stale, causing incorrect toggling behavior.Final Answer:
Timer input IN is assigned after checking T1.Q, causing logic error -> Option AQuick Check:
Assign timer input before reading output = A [OK]
- Assigning timer input after reading output
- Forgetting time units in PT
- Misunderstanding toggle logic
Solution
Step 1: Understand continuous flashing logic
To flash continuously, the light must toggle every timer cycle and the timer must restart each cycle.Step 2: Apply timer reset and toggle
Toggle light when timer finishes (Q=TRUE), then reset timer input to restart timing for next cycle.Final Answer:
Use a TON timer with 1 second preset and toggle light when timer done, then reset timer input -> Option AQuick Check:
Toggle + reset timer input = continuous flashing [OK]
- Not resetting timer input causing no repeat
- Using wrong preset time for desired flash rate
- Toggling light only once
Ready to prove it?
Pass the test and this pattern moves unknown
