just trigger one, then the other. Could someone advise whether bringing the pingpin back to low when the pingpin is in input mode is necessary? I figured it would be so that it still isn't reading while the next sensor is working. I added that into this code. This is untested, but it compiles. Let us know how it works.
const int pingPinLeft = 7;
const int pingPinRight = 8;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in centimeters:
long duration;
int cmL, cmR;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPinLeft, OUTPUT);
digitalWrite(pingPinLeft, LOW);
delayMicroseconds(2);
digitalWrite(pingPinLeft, HIGH);
delayMicroseconds(5);
digitalWrite(pingPinLeft, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPinLeft, INPUT);
duration = pulseIn(pingPinLeft, HIGH);
// convert the time into a distance
cmL = microsecondsToCentimeters(duration);
Serial.print(cmL);
Serial.print("cmLEFT");
Serial.println();
delay(100);
digitalWrite (pingPinLeft, LOW); // I don't know if this command is necessary, but it would make sense so it doesn't continue
// to read while the next sensor is working
//////////////////////////////////////////////////////
delay(1000); //Time between sensor readings to "clear the airwaves"... so to speak. Could be much less or maybe is not needed.
///////////////////////////////////////////////////////
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPinRight, OUTPUT);
digitalWrite(pingPinRight, LOW);
delayMicroseconds(2);
digitalWrite(pingPinRight, HIGH);
delayMicroseconds(5);
digitalWrite(pingPinRight, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPinRight, INPUT);
duration = pulseIn(pingPinRight, HIGH);
// convert the time into a distance
cmR = microsecondsToCentimeters(duration);
Serial.print(cmR);
Serial.print("cmRIGHT");
Serial.println();
delay(100);
digitalWrite (pingPinLeft, LOW); // I don't know if this command is necessary, but it would make sense so it doesn't continue
// to read while the next sensor is working
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}