hi, im doing a school project where i need to run 4 ultrasonic sensors with 4 active buzzers, making each ultrasonic sensor pair with each buzzer and im having a lot of problems like the sensors detecting 0 's and buzzers not turning off when i tell them too and all my wires are correct
its hard to explain but like i want the buzzers to turn on when the sensor reach 10cm but they just keep beeping even if theres nothing there because of the sensor detecting 0's
here is my code :
#define echopin1 13
#define trigpin1 12
#define echopin2 3
#define trigpin2 4
#define echopin3 8
#define trigpin3 7
#define echopin4 11
#define trigpin4 10
const int buzzer1 = 2;
const int buzzer2 = 5;
const int buzzer3 = 6;
const int buzzer4 = 9;
long duration1;
long duration2;
long duration3;
long duration4;
int distance1;
int distance2;
int distance3;
int distance4;
int safetyDistance1;
int safetyDistance2;
int safetyDistance3;
int safetyDistance4;
void setup()
{
Serial.begin(9600);
pinMode(trigpin1, OUTPUT);
pinMode(trigpin2, OUTPUT);
pinMode(trigpin3, OUTPUT);
pinMode(trigpin4, OUTPUT);
pinMode(echopin1, INPUT);
pinMode(echopin2, INPUT);
pinMode(echopin3, INPUT);
pinMode(echopin4, INPUT);
pinMode(buzzer1, OUTPUT);
pinMode(buzzer2, OUTPUT);
pinMode(buzzer3, OUTPUT);
pinMode(buzzer4, OUTPUT);
}
void loop()
{
digitalWrite(trigpin4, LOW);
delayMicroseconds (2);
digitalWrite(trigpin4, HIGH);
delayMicroseconds (10);
digitalWrite(trigpin4, LOW);
duration4 = pulseIn(echopin4, HIGH);
distance4 = duration4 * 0.034 / 2;
Serial.print("Distance4: ");
Serial.print(distance4);
Serial.println(" cm");
safetyDistance4 = distance4;
if (safetyDistance4 >= 10){
digitalWrite(buzzer4, HIGH);
}
else{
digitalWrite(buzzer4, LOW);
}
digitalWrite(trigpin3, LOW);
delayMicroseconds (2);
digitalWrite(trigpin3, HIGH);
delayMicroseconds (10);
digitalWrite(trigpin3, LOW);
duration3 = pulseIn(echopin3, HIGH);
distance3 = duration3 * 0.034 / 2;
Serial.print("Distance3: ");
Serial.print(distance3);
Serial.println(" cm");
safetyDistance3 = distance3;
if (safetyDistance3 >= 10){
digitalWrite(buzzer3, HIGH);
}
else{
digitalWrite(buzzer3, LOW);
}
digitalWrite(trigpin2, LOW);
delayMicroseconds (2);
digitalWrite(trigpin2, HIGH);
delayMicroseconds (10);
digitalWrite(trigpin2, LOW);
duration2 = pulseIn(echopin2, HIGH);
distance2 = duration2 * 0.034 / 2;
Serial.print("Distance2: ");
Serial.print(distance2);
Serial.println(" cm");
safetyDistance2 = distance2;
if (safetyDistance2 >= 10){
digitalWrite(buzzer2, HIGH);
}
else{
digitalWrite(buzzer2, LOW);
}
digitalWrite(trigpin1, LOW);
delayMicroseconds (2);
digitalWrite(trigpin1, HIGH);
delayMicroseconds (10);
digitalWrite(trigpin1, LOW);
duration1 = pulseIn(echopin1, HIGH);
distance1 = duration1 * 0.034 / 2;
Serial.print("Distance1: ");
Serial.print(distance1);
Serial.println(" cm");
safetyDistance1 = distance1;
if (safetyDistance1 >= 10){
digitalWrite(buzzer1, HIGH);
}
else{
digitalWrite(buzzer1, LOW);
}
delay (1000);
}
ik my code is a mess and wierd but i really need help with this if possible
(using arduino mega)