I have the following code reading 2 ping sensors, where the transmitter on each has been removed leaving only the receivers. The transmitter that was removed from sensor 1 was soldered back to the board with a 10ft length of wire. This will allow me to hold the transmitter 2ft from the 2 receivers on center. The transmitter is triggered by sensor 1 and initializes both sensor 1 and sensor 2. Each receiving sensor is spaced 18" apart and approx. 24" away from the transmitter on center. I able to read the output in cm on sensor 1, however the output on sensor 2 seems to be approx. 10cm short of sensor 1. I have tried changing the timing function and modifying the order of the code, but this does not change the outcome.
Can anyone offer any advice or see anything that my help match the output of the sensors at equal distance?
Thank you
Dave
Please find the code below.
#include <NewPing.h>
#define TRIGGER_PIN_1 12 // trigger pin on ping sensor 1
#define ECHO_PIN_1 12 // echo pin on ping sensor1
#define MAX_DISTANCE_1 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define TRIGGER_PIN_2 8 // trigger pin on ping sensor 2
#define ECHO_PIN_2 8 // echo pin on ping sensor 2
#define MAX_DISTANCE_2 175 // Maximum distance to ping in centimeters
NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE_1);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE_2);
unsigned int pingSpeed_1 = 300; // Frequency of ping output (in milliseconds)
unsigned long pingTimer_1; // Hold next ping time.
unsigned int pingSpeed_2 = 300; // Frequency of ping output (in milliseconds)
unsigned long pingTimer_2; // Hold next ping time.
void setup() {
Serial.begin(9600); // serial monitor
pingTimer_1 = millis(); // Start
pingTimer_2 = millis(); // Start
}
void loop() {
if (millis() >= pingTimer_1) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer_1 += pingSpeed_1; // Set next ping time.
sonar1.ping_timer(echoCheck1); // Send out ping, calls "echoCheck" function every 24uS to check ping status.
}
if (millis() >= pingTimer_2) { // pingSpeed milliseconds since last ping, do another ping.
pingTimer_2 += pingSpeed_2; // Set next ping time.
sonar2.ping_timer(echoCheck2); // Send out ping, calls "echoCheck" function every 24uS to check ping status.
}
// add other code with no delay
}
void echoCheck1() { // Timer2 interrupt calls function every 24uS to check the ping status.
// Don't do anything here!
if (sonar1.check_timer()) { // check to see if ping was received.
// Here's where you can add code.
Serial.print("Ping1: ");
Serial.print((sonar1.ping_result / US_ROUNDTRIP_CM); // convert to cm with US_ROUNDTRIP_CM
Serial.println("cm1");
}
// Don't do anything here!
}
void echoCheck2() { // Timer2 interrupt calls function every 24uS to check the ping status.
// Don't do anything here!
if (sonar2.check_timer()) { // check to see if ping was received.
// Here's where you can add code.
Serial.print("Ping2: ");
Serial.print(sonar2.ping_result / US_ROUNDTRIP_CM); // convert to cm with US_ROUNDTRIP_CM
Serial.println("cm2");
}
// Don't do anything here!
}