I have two non-working ultrasonic sensors but three ones. I plugged them into the same code and for the working one I got the correct result but for the non-working ones I got 0 and -71 and I am not sure how to fix them
PLEASE HELP!
int tEcho = A4;
int tTrig = A5;
// Ultrasonic sensor for the left side of the vehicle
int lTrig = 30;
int lEcho = 31;
int leftSideTrig = A15;
int leftSideEcho = A14;
// Ultrasoinc sensor for the right side of the vehicle
int rTrig = 38;
int rEcho = 39;
int distance_test(int echo, int trig){
int duration;
int distance;
digitalWrite(trig, LOW);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
void setup(){
pinMode(tTrig, OUTPUT);
pinMode(lTrig, OUTPUT);
pinMode(rTrig, OUTPUT);
pinMode(leftSideTrig, OUTPUT);
pinMode(tEcho, INPUT);
pinMode(lEcho, INPUT);
pinMode(rEcho, INPUT);
pinMode(leftSideEcho, INPUT);
Serial.begin(9600);
}
void loop(){
/*Serial.print("Left Side: ");
Serial.println(distance_test(leftSideEcho, leftSideTrig));*/
Serial.print("Left: ");
Serial.println(distance_test(lEcho, lTrig));
/*Serial.print("Front: ");
Serial.println(distance_test(tEcho, tTrig));
Serial.print("Right: ");
Serial.println(distance_test(rEcho, rTrig));
Serial.println();*/
}