Delay on Conveyor

I use the sketch below to detect pallets passing on a conveyor. Right now it has a delay to wait for the pallet to pass before resetting and waiting for the next. The problem is that if the conveyor stops while a pallet is front of the sensor it will detect the same pallet multiple times. I have found some things that should make the board wait no matter how long a pallet is in front of it but I can’t get it to work.What is the correct code to get the board to wait until an obstacle has passed?

// IR Obstacle Collision Detection Module
// Henry’s Bench

#include “Keyboard.h”

int isObstaclePin = 7; // This is our input pin
int isObstacle = HIGH; // HIGH MEANS NO OBSTACLE
char ctrl = KEY_LEFT_CTRL;
char alt = KEY_LEFT_ALT;

void setup() {
pinMode(isObstaclePin, INPUT);


void loop() {
isObstacle = digitalRead(isObstaclePin);
if (isObstacle == LOW)

If you think about it logically a HIGH to LOW transition means a pallet is detected, therefore a LOW to HIGH transition means the pallet has passed.

I would suggest removing the delays and use the transitions to determine the status of a pallet. This way a pallet can stop for an indefinite amount of time in front of the sensor and the code will only detect the pallet once.

Yes, you do not want to know when a pallet is present. You want to know when a pallet BECOMES present or perhaps when a pallet BECOMES absent.


That worked. Thank you very much.