Hi all,
Suppose I have a conveyor where boxes (pink and white) pass one by one. A sensor detects if a box is pink, and about 1600 mm further there’s a rejector that should push only the pink ones out.
Now, I don’t have the hardware set up yet I’m just trying to develop the logic.

My thought process is:
This way it doesn’t matter if boxes are spaced randomly or if the conveyor speed changes, since everything is position-based.
The main doubt I’m stuck on is overflow. If the encoder counter rolls over (say it’s 16-bit), what’s the clean way to still check if the current count has reached the target count?
For example, if current count = 65530 and I add distance = 100 pulses, the target becomes 65630, but the counter will overflow. How should I handle that comparison correctly?
Suppose I have a conveyor where boxes (pink and white) pass one by one. A sensor detects if a box is pink, and about 1600 mm further there’s a rejector that should push only the pink ones out.
Now, I don’t have the hardware set up yet I’m just trying to develop the logic.

My thought process is:
- An encoder is attached to the conveyor.
- When the sensor sees a pink box, I note the current encoder count.
- I add the distance-to-rejector (converted into encoder pulses) to get a target count.
- Then, as the conveyor moves, I keep checking the encoder count. When it matches the target, I would fire the rejector.
This way it doesn’t matter if boxes are spaced randomly or if the conveyor speed changes, since everything is position-based.
The main doubt I’m stuck on is overflow. If the encoder counter rolls over (say it’s 16-bit), what’s the clean way to still check if the current count has reached the target count?
For example, if current count = 65530 and I add distance = 100 pulses, the target becomes 65630, but the counter will overflow. How should I handle that comparison correctly?


