Why doesn't this work?

I wrote this sketch to read the ping delay of a HC-SRO4 ultrasonic distance sensor, and activate a buzzer whenever something got within a 500 millisecond delay of the sensor. My plan was to mount the ultrasonic sensor to the bottom of the bumper of my father’s car, which has a very low front chrome bumper, so that it would beep whenever he got too close. Please help! It always turns on the relay, no matter the status of the sensor. I double checked this by including a “print to serial” command. Any troubleshooting advice would be greatly appreciated.

Nathaniel

Ultrasonic_Sensor_2.ino (1.33 KB)

Please upload your wiring diagram. or check Arduino - Ultrasonic Sensor - Piezo Buzzer tutorial

The ping method returns NO_ECHO (which is 0) if there is no return. Since this meets your criteria in:

f (nextvariable<fivehundred)

the relay will be activated.

You might change the code to something like:

if (nextvariable != NO_ECHO && nextvariable<fivehundred)

and see if that changes things.

A round trip time of 500 ms from ping to echo detection would mean the distance was 85 meters (280 ft). Can your range finder detect the echo from a car sized object at that distance?

Oh... the sketch says 500 microseconds.

You should probably let the library translate to centimeters (.ping_cm()) ot inches (.ping_in()) for you. How many people know the round-trip distance for 500 microseconds? :slight_smile:

johnwasser:
Oh... the sketch says 500 microseconds.

You should probably let the library translate to centimeters (.ping_cm()) ot inches (.ping_in()) for you. How many people know the round-trip distance for 500 microseconds? :slight_smile:

Assuming the speed of sound = 767 mph = 343 metres per second, then I reckon the round trip would be about 8.5 cm = 3.3 inches. Rather close at 1.6 inches I’d have thought!