I used 2 sensors with the 15 Sensor example sketch, and found that what I needed to do, it was too slow at picking up changes.
I reverted back to adding 2 seperate sensors, and doing 2 seperate checks, See code below:
#include <NewPing.h>
#define SONAR_NUM 2 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar_START(6,6, MAX_DISTANCE);
NewPing sonar_END(7,7, MAX_DISTANCE);
unsigned int pingSpeed = 30; // How frequently are we going to send out a ping (in milliseconds). 50ms would be 20 times a second.
unsigned long pingTimer[2]; // +1 for timer that displays results.
void setup()
{
Serial.begin(9600);
pingTimer[0] = millis() + 50;
pingTimer[1] = millis() + 100;
/* Delays the start up per sensor, as I want to know the distance when it first starts up from the object - Running them at the same time causes issues. */
}
void loop()
{
if (millis() >= pingTimer[0]) {
pingTimer[0] += pingSpeed;
startCheck();
}
if (millis() >= pingTimer[1]) {
pingTimer[1] += pingSpeed;
endCheck();
}
}
void startCheck()
{
int cm = sonar_START.ping_cm();
//Doing some code here
}
void endCheck()
{
int cm = sonar_END.ping_cm();
//Doing some other code here
}
The above method, works quicker and more reliable than using the code from the 15 sketch example with changing NUM_SONOR to 2