3 ultrasonic sensors, only 1 works properly

I connect 3 ultrasonic sensors, but the last one is working properly. There is no problem with the circuit, I tried everything. here are the codes

#define trigPin1 2 
#define echoPin1 3 
#define trigPin2 4 
#define echoPin2 5 


#define buzzer 9 
#define potentiometer A0

int valueOfPot;
int frequenceOfBuzzer;

long duration, distance, RightSensor,LeftSensor;

void setup()
{
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);


  pinMode(buzzer, OUTPUT);
  pinMode(potentiometer, INPUT);
}

void loop() {
  /*Buzzer Control*/
  valueOfPot = analogRead(potentiometer);
  frequenceOfBuzzer = map(valueOfPot,0,1023,0,900);
  
  SonarSensor(trigPin1, echoPin1);
  RightSensor = distance;
  SonarSensor(trigPin2, echoPin2);
  LeftSensor = distance;

  Serial.print(LeftSensor);
  Serial.print("cm - ");
  Serial.print(RightSensor);
  Serial.println("cm");

   /*Ultrasonic1*/
 if (distance > 30 and distance < 62) { 
    tone(buzzer, frequenceOfBuzzer, frequenceOfBuzzer);
 }
 else if (distance > 0 and distance < 31) {
    tone(buzzer, frequenceOfBuzzer); 
 }
  else {
    noTone(buzzer);
  }
    
  delay(1000);
}

void SonarSensor(int trigPin,int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

}

I see only two sensors, but I'd say out a longer pause between the two pings, so the aggregate ping rate should be no more than 20 to 30 Hz

how can I do that? Any chance you can change the codes and explain?

Just put a short delay between your two readings.

nothing happened.

Perhaps a longer delay, and put it after every reading - try 70ms - the ultrasound pulse will be bouncing all round the room for a while, you need the environment to ring-down enough before the next ping. Sounds only travels 24m in 70ms, ie a bounce from 12m away will be coming back even with that delay, but that ought to be very attenuated due to the distance.

delay is negligible. I just put a delay between 3 sensors. like this

firstsensor();
delay(200);
secondsensor();
delay(200);
thirdsensor();
delay(200);

thank you all for your help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.