Hello, I am using two ultrasonic sensors and two try and measure distance, They both run off the same trigger on pin 4 and then their input is read in on pin 5 for the first sensor and pin 6 for the second sensor. However my second sensor is not giving me any feed back. Is there a error in my code that i am not seeing?
Any help is appreciated, Thank you!
const int TrigPin = 4; //Triger for both sensors
const int Echo1Pin = 5; //Reads Echo from 1st sensor
const int Echo2Pin = 6; //Reads Echo from 2nd sensor
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop()
{
long duration1, duration2, inches1, cm1, inches2, cm2;
pinMode(TrigPin, OUTPUT);
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);
pinMode(Echo1Pin, INPUT);
pinMode(Echo2Pin, INPUT);
duration1 = pulseIn(Echo1Pin, HIGH);
duration2 = pulseIn(Echo2Pin, HIGH);
inches1 = microsecondsToInches1(duration1);
cm1 = microsecondsToCentimeters1(duration1);
inches2 = microsecondsToInches2(duration2);
cm2 = microsecondsToCentimeters2(duration2);
Serial.print(inches1);
Serial.print("in1, ");
Serial.print(cm1);
Serial.print("cm1, ");
Serial.print(duration1);
Serial.print("duration1");
Serial.println();
delay(100);
Serial.print(inches2);
Serial.print("in2, ");
Serial.print(cm2);
Serial.print("cm2, ");
Serial.print(duration2);
Serial.print("duration2");
Serial.println();
delay(100);
}
long microsecondsToInches1(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;
}
long microsecondsToCentimeters1(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;
}
long microsecondsToInches2(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;
}
long microsecondsToCentimeters2(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;
}