if i put in the code below, i only get the value for 1 of the 2 ultrasonic sensor. The other sensor gives a 0 value continuously.
I need both of the measured distance value. what should i do?
const int ping1Pin = 9; // Trigger Pin of Ultrasonic Sensor
const int echo1Pin = 10; // Echo Pin of Ultrasonic Sensor
const int ping2Pin = 11; // Trigger Pin of Ultrasonic Sensor
const int echo2Pin = 12; // Echo Pin of Ultrasonic Sensor
void setup() {
// put your setup code here, to run once:
// ultrasoon
pinMode(ping1Pin, OUTPUT);
pinMode(echo1Pin, INPUT);
pinMode(ping2Pin, OUTPUT);
pinMode(echo2Pin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
// uitlezen ultrasoon
long duration1, x1;
digitalWrite(ping1Pin, LOW);
delayMicroseconds(2);
digitalWrite(ping1Pin, HIGH);
delayMicroseconds(10);
digitalWrite(ping1Pin, LOW);
duration1 = pulseIn(echo1Pin, HIGH);
x1 = microsecondsToMillimeters(duration1);
delay(10);
long duration2, x2;
digitalWrite(ping2Pin, LOW);
delayMicroseconds(2);
digitalWrite(ping2Pin, HIGH);
delayMicroseconds(10);
digitalWrite(ping2Pin, LOW);
duration2 = pulseIn(echo2Pin, HIGH);
x2 = microsecondsToMillimeters(duration2);
delay(10);
} // EINDE HOOFDPROGRAMMA
long microsecondsToMillimeters(long microseconds) {
return microseconds / 2.9 / 2;
}