Having problems with my ultrasonic sensor and active buzzes

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)

When you wrote the program, did you study the documentation on pulseln()?

no because this never happened before and when i was doing the code i tested 1 by 1 and it work just fine but with the 4 sensors at the same time i had 2 sensors giving 0 values
but i guess need to study that for sure

les 2 capteurs donnent toujours 0 ou alors ils détectent parfois 0 ?
Dans le deuxième cas j'aurais ajouté un ligne if comme ceci même si ce n'est pas très propre

if (safetyDistance4 >= 10){
if (safetyDistance4 != 0){
 digitalWrite(buzzer4, HIGH);
  }
else{
digitalWrite(buzzer4, LOW);
}
  }
else{
  digitalWrite(buzzer4, LOW);
  }

Then you would know why you get zeros!

the sensor gives 0's randomly because some times they work and give the exact number even when theres an object in front

and responding to the code, i just tested it and the buzzers are still beeping with 0

@frambroise has a question in the translation from post #4 is:

"The 2 sensors always give 0 or do they sometimes detect 0? In the second case I would have added an if line like this even though it's not very clean"

And maybe, try putting a slight delay between each sensor routine. Maybe one SR04 has a stronger signal than the others and is stepping on the other signals? (this is one way RADAR jamming works)

The pulseIn() function reads a HIGH or a LOW pulse on a pin. It accepts as arguments the pin and the state of the pulse (either HIGH or LOW). It returns the length of the pulse in microseconds. The pulse length corresponds to the time it took to travel to the object plus the time traveled on the way back. To calculate the distance to an object using the speed of sound.

distance = (traveltime/2) x speed of sound

The speed of sound is: 343m/s = 0.0343 cm/uS = 1/29.1 cm/uS Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS

And what does your documentation say is returned when a time-out occurs?

Hi Everyone, thank u all for the help!

I have finnaly manage to solve the problem and it was actually very stupid

aperantly it was 2 broken wires in each ultra sonic sensor (my 1 and 4 sensor) and that was the reason for them to detect 0 and for the buzzers to beep infinitly and i just noticed that because some times the sensor would work when i touched them a little

so yeah thanks!!!