Timer preset and accumulated values in PLC Programming - Time & Space Complexity
We want to understand how the time it takes to run a timer program changes as we change the timer settings.
Specifically, how the preset and accumulated values affect the program's execution time.
Analyze the time complexity of the following timer code snippet.
// Timer example
Timer T1;
T1.Preset = 1000; // Set timer preset value
T1.Accum = 0; // Reset accumulated time
while (T1.Accum < T1.Preset) {
T1.Accum += 1; // Increment accumulated time
}
// Timer done when Accum reaches Preset
This code simulates a timer counting up until it reaches the preset value.
Look at what repeats in the code.
- Primary operation: Incrementing the accumulated value inside the loop.
- How many times: The loop runs until accumulated equals preset, so it repeats as many times as the preset value.
The time to finish grows directly with the preset value.
| Input Size (Preset) | Approx. Operations (Loop Iterations) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: If you double the preset, the loop runs twice as long.
Time Complexity: O(n)
This means the time to complete grows linearly with the preset value.
[X] Wrong: "The timer runs in constant time no matter the preset."
[OK] Correct: The loop runs once for each unit up to the preset, so bigger presets take more time.
Understanding how timers work and how their settings affect program time helps you write efficient control programs and explain your logic clearly.
"What if the timer increments by 2 each loop instead of 1? How would the time complexity change?"
Practice
preset value of a timer represent in PLC programming?Solution
Step 1: Understand timer preset meaning
The preset value is the target time set for the timer to reach before it triggers.Step 2: Differentiate from accumulated value
The accumulated value shows elapsed time, not the target time.Final Answer:
The total time the timer should count before activating -> Option CQuick Check:
Preset = target time [OK]
- Confusing preset with accumulated value
- Thinking preset changes during timing
- Assuming preset is a delay before start
Solution
Step 1: Identify correct property for preset
In PLC timers, the preset value is usually set with the property named 'PRESET'.Step 2: Check other options
'ACC' is for accumulated time, 'SET' and 'TIME' are not standard properties for preset.Final Answer:
TIMER.PRESET = 5000 -> Option AQuick Check:
Preset set with .PRESET property [OK]
- Using .ACC to set preset
- Using non-standard property names
- Confusing preset with timer start command
TIMER.PRESET = 10000TIMER.ACC = 7500What is the timer's current state?
Solution
Step 1: Compare accumulated and preset values
The accumulated value (7500) is less than the preset (10000), so the timer has not reached its target.Step 2: Determine timer state
Since accumulated is less than preset, the timer is still counting and not done.Final Answer:
Timer is still counting and not done -> Option DQuick Check:
ACC < PRESET means timer running [OK]
- Assuming timer is done when ACC < PRESET
- Confusing paused with running
- Thinking reset means ACC is non-zero
Solution
Step 1: Understand timer behavior when enabled
The accumulated value increases only when the timer is enabled and counting.Step 2: Analyze causes for no increase
If the timer is not enabled or triggered, the accumulated value stays the same.Final Answer:
The timer is not enabled or triggered properly -> Option BQuick Check:
Timer must be enabled to accumulate time [OK]
- Assuming preset zero stops accumulation
- Ignoring timer enable condition
- Thinking ACC resets automatically
Solution
Step 1: Understand timing requirement
The process must start only after 15 seconds have fully passed.Step 2: Use timer values to control start
Starting the process when accumulated time is equal or greater than preset ensures 15 seconds elapsed.Final Answer:
Set TIMER.PRESET = 15000 and start process when TIMER.ACC >= TIMER.PRESET -> Option AQuick Check:
Start after ACC >= PRESET for exact delay [OK]
- Starting process immediately on timer start
- Starting before accumulated time reaches preset
- Using ACC = 0 as start condition
Ready to prove it?
Pass the test and this pattern moves unknown
