Parts counter with conveyor in PLC Programming - Time & Space Complexity
We want to understand how the time it takes to count parts on a conveyor changes as more parts pass by.
How does the program's work grow when the number of parts increases?
Analyze the time complexity of the following code snippet.
// Parts counter with conveyor
VAR
partCount : INT := 0;
partDetected : BOOL := FALSE;
END_VAR
IF SensorInput = TRUE THEN
IF partDetected = FALSE THEN
partCount := partCount + 1;
partDetected := TRUE;
END_IF;
ELSE
partDetected := FALSE;
END_IF;
This code counts parts passing a sensor on a conveyor by detecting when a part arrives and ensuring it counts each part only once.
- Primary operation: Checking the sensor input and updating the part count.
- How many times: This check happens continuously, once every scan cycle of the PLC.
The program checks the sensor the same number of times regardless of how many parts pass.
| Input Size (number of parts) | Approx. Operations |
|---|---|
| 10 | Same number of sensor checks as scan cycles |
| 100 | Same number of sensor checks as scan cycles |
| 1000 | Same number of sensor checks as scan cycles |
Pattern observation: The work per scan cycle stays constant, so total work grows linearly with time, not with the number of parts.
Time Complexity: O(1)
This means each scan cycle takes the same amount of time no matter how many parts have passed.
[X] Wrong: "The program takes longer as more parts pass because it counts each one."
[OK] Correct: The program only checks the sensor once per scan cycle and updates the count immediately, so the time per cycle does not increase with more parts.
Understanding how continuous sensor checks work helps you explain real-time control systems clearly and confidently.
"What if the program stored every part's timestamp in a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of the parts counter
The parts counter uses a sensor to detect each part passing on the conveyor.Step 2: Identify the action after counting
Once the set number of parts is reached, the conveyor stops to control production.Final Answer:
To count the number of parts passing a sensor and stop the conveyor after a set count -> Option AQuick Check:
Parts counter purpose = Count and stop conveyor [OK]
- Confusing counting with sorting
- Thinking the counter speeds up conveyor
- Assuming it cleans the conveyor
Solution
Step 1: Identify how counters reset in PLCs
Counters are reset by setting their value back to zero using a reset instruction.Step 2: Eliminate incorrect options
Increasing the counter or stopping conveyor does not reset the count; disconnecting sensor stops detection but not reset.Final Answer:
Set the counter value to zero using a reset instruction -> Option DQuick Check:
Reset counter = set to zero [OK]
- Confusing reset with increment
- Stopping conveyor does not reset count
- Disconnecting sensor is not a reset
IF Sensor = TRUE THEN
Counter = Counter + 1
END IF
IF Counter >= 10 THEN
Conveyor = OFF
END IF
What will be the state of the conveyor after 10 parts pass the sensor?
Solution
Step 1: Analyze the counter increment
Each time the sensor is TRUE, the counter increases by 1 until it reaches 10.Step 2: Check conveyor control condition
When counter is 10 or more, the conveyor is turned OFF.Final Answer:
Conveyor turns OFF -> Option CQuick Check:
Counter >= 10 stops conveyor [OK]
- Thinking conveyor stays ON after 10 parts
- Assuming counter resets automatically
- Believing sensor stops detecting
IF Sensor = TRUE THEN
Counter = Counter + 1
END IF
IF Counter = 10 THEN
Conveyor = OFF
Counter = 0
END IF
What problem might occur?
Solution
Step 1: Review counter reset timing
The counter resets to zero immediately after reaching 10, possibly before conveyor stops.Step 2: Understand effect on conveyor
If counter resets too soon, the conveyor OFF condition may not hold, causing conveyor to continue running.Final Answer:
Counter resets before conveyor stops, so conveyor never stops -> Option AQuick Check:
Resetting counter too early breaks stop logic [OK]
- Resetting counter immediately after reaching target
- Using '=' instead of '>=' causing missed conditions
- Ignoring sensor input usage
Solution
Step 1: Set condition to check counter value
The program must check if the counter has reached or exceeded 50 parts.Step 2: Stop conveyor and then reset counter
After stopping the conveyor, reset the counter to zero to prepare for next batch.Final Answer:
Check if counter >= 50, then stop conveyor, then reset counter -> Option BQuick Check:
Stop then reset after count reached [OK]
- Resetting counter before checking count
- Stopping conveyor without checking count
- Incrementing counter only after stopping conveyor
Ready to prove it?
Pass the test and this pattern moves unknown
