Problem with my ultrasonic sensor

Hi I am a beginner, so I may have obvious mistakes.

I am struggling to make a mechanism which can detect the movement and send a voice signal by buzzer when it detects a movement.

My buzzers is keep working even though there is no movement.

Here is my code, thanks in advance:

#include <SoftwareSerial.h>

//ultrasonic pins
int trigger = 9;
int echo = 8;
long duration;
int distance;
long duration2;
int distance2;
int buzzer = 7;
int led = 2;

// motor pins
int lDIR = 13;
int lPWD = 3;
int rDIR = 12;
int rPWD = 11;

void setup() {
pinMode(trigger, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(lDIR, OUTPUT);
pinMode(lPWD, OUTPUT);
pinMode(rDIR, OUTPUT);
pinMode(rPWD, OUTPUT);
pinMode(echo, INPUT);

}

void loop(){

motorStop();
delay(5);
getDistance();
delay(5000);

if (distance > distance2) {
runBuzzer();

}

else if (distance < distance2) {
runBuzzer();
}

else {
runLed();

}

delay(5000);
}

long getDistance() {
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delayMicroseconds(5000);
digitalWrite(trigger, LOW);
duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH);
delayMicroseconds(5000);
digitalWrite(trigger, LOW);
duration2 = pulseIn(echo, HIGH);
distance2 = duration2 * 0.034 / 2;
}

void runBuzzer(){
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1000); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec

}

void motorStop() {
digitalWrite(lDIR, ILERI);
digitalWrite(rDIR, ILERI);
analogWrite(lPWD, 0);
analogWrite(rPWD, 0);
}

void runLed(){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

If you've got two ultrasonic rangers, I'd slow down the aggregate ping rate.

Please remember to use code tags when posting code.

(Code needs a MASSIVE tidy-up)

Actually there are not two, I couldn't figure out how to detect the movement.

Sorry,I just saw two distance variables and assumed you had two sensors - didn't notice that you had only one set of trigger/echo pins.

I don't know why you'd do that.

That seems awfully long.

If you trigger the sensor again immediately after the first reading, it is possible that the second reading will be fooled by a late echo from the first reading. You should delay about 30 milliseconds between readings.

a quick scan of your code I see you are pinging your sensor twice after a 5ms delay, then comparing the two measured distances--that sounds reasonable.

However in your loop processing I see

if (distance > distance2) {
runBuzzer();
}
else if (distance < distance2) {
runBuzzer();
}

Meaning unless distance = distance2 the buzzer will always ring. These sensors are not going to give the same exact distance on multiple reads. Note: I have these sensors mounted to a wall in my garage measuring the distance between the wall and bumper (so I can park always 10 inches away. I average 5 readings every 100ms or so, and still i'll get +/- 1 inch of movement event when the car is stopped.

One way to fix this is to have some threshold distance (I use 2 inches and seems to work well)

something like

if (abs(distance - distance2) > 2) {
// where 2 is your allowable distance before buzzer goes off
// i'm not sure what units distance is but need at least a few inches
runBuzzer();
}

also, maybe use floats for distance and distance 2 so you can get a slightly more accurate distance? Make sure you force all calcs to force float... 0.034 / 2 will return and int use 0.034/2.0

Hope this helps

That doesn't sound reasonable at all - a 200Hz ping rate is probably unsustainable, with the late returns problem noted above.

No. When one side of the divide is a float and the other side is an int, the int gets converted to float. See 'usual arithmetic conversions':
https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions