Hi,
I am using 4 ultrasonic sensors in my project and NewPing method, can’t get the right readings, need help please.
Thank
Adam
// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define SONAR_NUM 3 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(2, 3, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(4, 5, MAX_DISTANCE),
NewPing(7, 8, MAX_DISTANCE)
};
uint8_t duration[3] = {0};
uint8_t distance[3] = {0};
#define iterations 3 // Number of sensors.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print(i);
Serial.print("=");
Serial.print(sonar[i].ping_cm());
Serial.println("cm ");
duration[i] = sonar[i].ping_median(iterations);
Serial.print("duration[i]=");
Serial.println(duration[i]);
distance[i] = (duration[i] / 29 / 2);
Serial.print("distance[i]=");
Serial.println(distance[i]);
}
// Serial.println();
}