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
#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");
}
}