teckel:
How many pins are you using? 4? One pin for triggers on all sensors and 3 pins to get the results from each sensor? If so, you could just use 3 pins by using the method I describe.Tim
Please forgive me Tim, I am not understanding, I am using four pins as you have stated but I don't understand how to wire the sensor to only use three pins. This surely would be better because I can add a forth sensor pointing to the rear.
Here is the setup code I am presently using.
#define LED_STATUS_PIN 13
#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 300 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 66 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
...
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
byte currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(7, 10, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(7, 11, MAX_DISTANCE),
NewPing(7, 5, MAX_DISTANCE),
...
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) { // Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1)
{
oneSensorCycle();
serialCom();
} // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = i; // Sensor being accessed.
cm[currentSensor] = 999; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}