Washing machine control sequence in PLC Programming - Time & Space Complexity
We want to understand how the time a washing machine control program takes changes as the number of washing steps increases.
Specifically, we ask: How does the program's running time grow when we add more washing cycles?
Analyze the time complexity of the following code snippet.
PROGRAM WashingMachineControl
VAR
step : INT := 0;
totalSteps : INT := 5;
END_VAR
FOR step := 1 TO totalSteps DO
// Execute washing step
ExecuteStep(step);
END_FOR
END_PROGRAM
This code runs a fixed number of washing steps in order, calling a function for each step.
- Primary operation: The FOR loop that runs through each washing step.
- How many times: It runs once for each step, from 1 up to totalSteps.
As the number of washing steps increases, the program runs more calls to ExecuteStep, one per step.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 calls to ExecuteStep |
| 10 | 10 calls to ExecuteStep |
| 100 | 100 calls to ExecuteStep |
Pattern observation: The number of operations grows directly with the number of steps.
Time Complexity: O(n)
This means the running time increases in a straight line as the number of washing steps grows.
[X] Wrong: "The program runs in constant time because each step is simple."
[OK] Correct: Even if each step is simple, the total time depends on how many steps there are, so time grows with the number of steps.
Understanding how loops affect running time helps you explain how control programs scale when adding more tasks, a useful skill in automation and control system design.
"What if we nested another loop inside the washing steps to repeat rinsing multiple times? How would the time complexity change?"
Practice
Solution
Step 1: Understand washing machine steps
The washing machine runs steps like filling, washing, rinsing, and spinning, each needing a set time.Step 2: Role of timers in automation
Timers control how long each step runs automatically without manual intervention.Final Answer:
To control how long each washing step runs -> Option DQuick Check:
Timers = control step duration [OK]
- Confusing timers with manual switches
- Thinking timers detect door status
- Assuming timers measure temperature
Solution
Step 1: Identify PLC ladder logic style
PLC ladder logic uses conditions like IF timer_done THEN to move to next step.Step 2: Match correct syntax
IF timer_done THEN next_step = TRUE uses a simple IF condition to set the next step flag, matching PLC logic style.Final Answer:
IF timer_done THEN next_step = TRUE -> Option AQuick Check:
IF timer_done THEN next_step [OK]
- Using programming loops not typical in PLC ladder logic
- Confusing switch-case with PLC logic
- Using syntax from other programming languages
step = 1
if step == 1 and timer_done:
step = 2
elif step == 2 and timer_done:
step = 3
elif step == 3 and timer_done:
step = 4
What will be the value of
step after three timer_done events?Solution
Step 1: Trace step changes on timer_done events
Initially step=1. After first timer_done, step=2. After second, step=3. After third, step=4.Step 2: Confirm final step value
After three timer_done events, step reaches 4.Final Answer:
4 -> Option BQuick Check:
3 timer_done events advance step to 4 [OK]
- Stopping count at 3 instead of 4
- Confusing elif conditions
- Ignoring step update after each event
if step == 1 and timer_done:
step = 2
if step == 2 and timer_done:
step = 3
if step == 3 and timer_done:
step = 4
What problem can occur during execution?
Solution
Step 1: Analyze if statements without else
All if statements run independently, so if timer_done is true, multiple steps can update in one cycle.Step 2: Understand effect on step variable
Step can jump from 1 to 4 in one cycle, skipping intermediate steps.Final Answer:
Multiple steps may be skipped in one cycle -> Option AQuick Check:
Separate ifs cause multiple step changes [OK]
- Assuming only one if runs per cycle
- Ignoring need for exclusive conditions
- Thinking timer_done is unchecked
Solution
Step 1: Understand safety requirement
Spinning should only happen if the door is closed to avoid accidents.Step 2: Choose logic that checks door status before spinning
if door_closed and step == 'spin' and timer_done: proceed to next step checks door_closed before allowing spinning step to proceed, ensuring safety.Final Answer:
if door_closed and step == 'spin' and timer_done: proceed to next step -> Option CQuick Check:
Door closed required before spin step [OK]
- Using OR instead of AND allowing unsafe spin
- Skipping spin step without proper condition
- Stopping machine incorrectly on door status
Ready to prove it?
Pass the test and this pattern moves unknown
