Help with code for parking sensors (one of sensors 'out of range')

Hi
I have been using a tutorial for reverse parking sensors for my engineering college project (link: http://www.deviceplus.com/how-tos/arduino-guide/how-to-make-parking-assist-car-sensors/). It is made using an Arduino UNO, two HC-SR04 sensors and two Piezo buzzers. I have run into an issue with the code in which only one of the two ultrasonic sensors is receiving data; the other is reporting as permanently ‘out of range’. We have tried swapping the sensors over, and it is not an issue of a faulty sensor. I have had help from a couple of people now who weren’t sure how to rectify the issue. I urgently need help correcting the code (posted below) as my project is due soon. Any help would be greatly appreciated. I have also attached a picture of the sensors.
Cheers :slight_smile:

#define trigPin1 13
#define echoPin1 12
#define buzzerPin1 6
#define trigPin2 11
#define echoPin2 10
#define buzzerPin2 7

void setup() {
 Serial.begin (9600);
 pinMode(trigPin1, OUTPUT); 
 pinMode(echoPin1, INPUT);
 pinMode(buzzerPin1, OUTPUT);
 pinMode(trigPin1, OUTPUT);
 pinMode(echoPin2, INPUT);
 pinMode(buzzerPin2, OUTPUT);
 pinMode(2, OUTPUT);
 digitalWrite(2,LOW);
}
void loop() {
 calculateDistance(echoPin1, trigPin1, buzzerPin1);
 calculateDistance(echoPin2, trigPin2, buzzerPin2);
}
void calculateDistance(int echo, int trigger, int buzzer){
 long duration, distance;

 digitalWrite(trigger, LOW);
 delayMicroseconds(2);
 digitalWrite(trigger, HIGH);

 delayMicroseconds(10);
 digitalWrite(trigger, LOW);

 duration = pulseIn(echo, HIGH);

 distance = (duration/2)/29.1;

 if(distance >= 200 | distance <= 0){
    Serial.println("Out of range");
 }else {
       tone(buzzer,2000,25);
       delay(distance*10);
       Serial.print(distance);
       Serial.println("cm");
}

 
}

  1. read the sticky on top of every section of this forum, so you know how to
  2. post your code properly, the
  3. other info you have to provide for your question, to
  4. post in an appropriate section to post your questions, and
  5. not to cross post.

Sorry, I'm new to all this

I thought I'd already responded to this, or a very similar thread, but anyway, you need to leave a longer time gap between readings.
Try that first.

Thanks for the suggestion, but the issue seems to be that the Arduino is not communicating with one of the sensors at all, thus the time between readings hasn't made any difference to the issue.

Switch the sensors - make sure the sensor is not faulty.

Hi,
Write some code just to run the sensors and check them one at a time.

Tom... :slight_smile: