Hi, I'm using ArduinoMega to use multiple ultrasonic sensors but I having some problems.
When I connect only 1 ultrasonic sensor, it works perfectly. However, with two or more ultrasonic sensors, the sensor values become 0.
Can anyone help me?
Thanks in advance.
Here is my code....
const int pingPin[2] = {5,6};
void setup() {
Serial.begin(9600);
}
void loop() {
for(int i =0; i<sizeof(pingPin)/sizeof(pingPin[0]); i++) {
int cm = ping(pingPin*) ;*
-
Serial.print(i); Serial.print(":"); Serial.print(cm); Serial.print(" ");*
-
}*
-
Serial.println();*
-
delay(100 ); // each centimeter adds 10 milliseconds delay digitalWrite(ledPin, LOW);*
}
int ping(int pingPin) { -
long duration, cm;*
-
pinMode(pingPin, OUTPUT);*
-
digitalWrite(pingPin, LOW);*
-
delayMicroseconds(2);*
-
digitalWrite(pingPin, HIGH);*
-
delayMicroseconds(5);*
-
digitalWrite(pingPin, LOW);*
-
pinMode(pingPin, INPUT);*
-
duration = pulseIn(pingPin, HIGH);*
-
cm = microsecondsToCentimeters(duration);*
-
return cm ;*
}
long microsecondsToCentimeters(long microseconds) { -
return microseconds / 29 / 2;*
}