So I have 3 Ping)) sensors... I have 2 set up as receivers and 1 as a transmitter. I'm trying to get the distance(inches) for both of them on the serial monitor, but I keep receiving only 1 of the distances and not the other, but when I turn off one of them the other works. The problem seems to be when I need to read the signal from the PINGS))) it only reads one, what am I doing wrong?
Here is my code:
const int pingPin = 7;
const int pingPin1= 4;
const int pingPin2=2;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, duration1, inches1, inches;
// 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(pingPin, OUTPUT);
pinMode(pingPin1, OUTPUT);
pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin, LOW);
digitalWrite(pingPin1, LOW);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
digitalWrite(pingPin1, HIGH);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
digitalWrite(pingPin1, LOW);
digitalWrite(pingPin2, 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(pingPin, INPUT);
pinMode(pingPin2, INPUT);
duration1 = pulseIn(pingPin2, HIGH); //THE PROBLEM SEEMS TO BE HERE
duration = pulseIn(pingPin, HIGH); //THE PROBLEM SEEMS TO BE HERE
// convert the time into a distance
inches = microsecondsToInches(duration);
delay(50);
inches1 = microsecondsToInches(duration1);
delay(50);
Serial.print(inches);
Serial.print("in, ");
Serial.print(inches1);
Serial.print("in2, ");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
Moderator edit: [code] [/code] tags added.