Ultrasonic sensor trigger LED only when blocked

I’m working on a project which has falling objects non stop, I only want to detect when objects block the sensors set trigger area. Any suggestions would be great!

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin1 9 // LED 1

int maximumRange = 300; // Maximum range needed
int minimumRange = 10; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin1, OUTPUT);


void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);

digitalWrite(trigPin, HIGH);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

//If the distance is beyond the limits - then nothing happens ie, more than 3 meters or less than 10 cm
if (distance >= maximumRange || distance <= minimumRange) {
Serial.println(“Nothing Happens”);
digitalWrite(LEDPin1, LOW);
} else {
digitalWrite(LEDPin1, HIGH);

Does your code compile? Does it do what you want? If not, what’s wrong? And if objects are falling “non-stop,” won’t you miss an object while you delay for 1/4 second?


PS: in the future, use the code tags (the </> button) to post your code.

Objects fall every 20sec approx. Every time an object passes through my target range the led turns on. I'm wonder how to either write a delay or if else statement that would only trigger led to light up in the event these objects start to pile up. So say my target range is blocked for more than 15 sec... then the led would light up and stay illuminated until path becomes clear

Thank you for the tip about code tags!!

How long does an object spend in front of the sensor? The fastest you should poll an ultrasonic sensor is around 20 to 30 Hz.

Poll the sensor every 15 seconds or whatever.

Use a flag to keep track of whether it was blocked or not the last time it was read.

If it is blocked when you read it and it was blocked the last time it was read (that is, it has been blocked two times in a row), then your stuff is probably piling up, so trigger your light and set off your alarms.

You might get an occasional false alarm if the sensor happens to be blocked by a falling object two times in a row. What are the odds of that? If the odds are unacceptably high, then every 15 seconds (or whatever), poll the sensor twice (with a short delay between reads) and consider it "blocked" only if it is blocked both times. The short delay should be about two or three times the time it takes for an object to pass the sensor.

The falling object takes less than 300ms to pass through the target window. I really appreciate the great responses. As, far as using a flag.. due you mean like a program interrupt? If you are any one wouldn't mind if they have the time...help me out with a skeleton sketch which would include the flags, delays..ect. In the mean time I will read up on flags..

Thanks again!!!

No, a “flag” is a variable that is either true or false, 1 or 0, preferably aptly named. For example: blockedLastRead or blkdLstRd

I usually use either boolean or byte for flags.

Why don’t you write a skeleton (pseudo-code) sketch based on the second and third paragraphs in post #4. Run through the operation in your mind (or on paper) and see if it does what you want. Then write the sketch in the Arduino IDE, see if it compiles, give it a test, and see if it works in real life.

Then add the complexity in the fourth paragraph, if warranted (and it is probably not warranted if you’re certain your objects drop every 20 seconds, you are polling your sensor every 15 seconds, and they only take 300 ms to pass by the sensor).