NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7

duxiaoshi:
great job... i use the library in my project and test my robot.
i have 15 SRF05 sonar sensors on the bot. I set pingSpeed 200ms, and pingInterval 35ms, but sometimes i get big value or negative value.

The ping12 result like this :

Ping12: 3328cm
Ping12: 12288cm
Ping12: -24544cm

my code is not simple and clean, and i'd like to know how to use NewPing to slove 15 sonar sensors problem

15 sensors! Now THAT'S what I'm talking about as a perfect use for the NewPing library!

First, is this only happening for sensor 12? If so, that could isolate the problem to something wrong with that sensor.

In any case, I believe the problem is that you're setting up the pings to happen 35ms apart, but then starting the loop process again in only 100ms. This works for 2 sensors, as it only takes 352=70ms per cycle through all the sensors. But, with 15, it would take at least 525ms to cycle through all 15 sensors (3515=525). Also, as AWOL stated, making an array of sonar objects makes the code a LOT shorter. I also noticed you're not using the "time" variable and the "cm" array should be set outside loop(). Below is a streamlined version of your sketch. I also converted it to use the timer event so it's more event driven:

#include <NewPing.h>

#define SONAR_NUM     15
#define MAX_DISTANCE 200
#define PING_INTERVAL 35 // Milliseconds between each sensor ping (35ms is about the minimum to avoid cross-sensor echos.  You could try making this as low as 29ms and see what happens).

const int pingCycle = PING_INTERVAL * (SONAR_NUM + 1); // This used to be PING_SPEED, pings as fast as possible.
unsigned long pingTimer[SONAR_NUM + 1]; // +1 for timer that displays results.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0;  // Keeps track of which sensor is active.

NewPing sonar[SONAR_NUM] = {
  NewPing(41, 42, MAX_DISTANCE),
  NewPing(43, 44, MAX_DISTANCE),
  NewPing(45, 20, MAX_DISTANCE),
  NewPing(21, 22, MAX_DISTANCE),
  NewPing(23, 24, MAX_DISTANCE),
  NewPing(25, 26, MAX_DISTANCE),
  NewPing(27, 28, MAX_DISTANCE),
  NewPing(29, 30, MAX_DISTANCE),
  NewPing(31, 32, MAX_DISTANCE),
  NewPing(34, 33, MAX_DISTANCE), //10
  NewPing(35, 36, MAX_DISTANCE), //11
  NewPing(37, 38, MAX_DISTANCE),
  NewPing(39, 40, MAX_DISTANCE),
  NewPing(50, 51, MAX_DISTANCE),
  NewPing(52, 53, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    pingTimer[i+1] = pingTimer[i] + PING_INTERVAL;
  }
}

void loop() {
  for (uint8_t i = 0; i <= SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += pingCycle; // Set next time this sensor will be pinged.
      if (i == SONAR_NUM) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      else {
        sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping.
        currentSensor = i; // Sensor being accessed.
        cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
        sonar[currentSensor].ping_timer(echoCheck); // Do the ping and wait for the echo interrupt.
      }
    }
  }
  // The rest of your code would go here.
}

void echoCheck() {
  if (sonar[currentSensor].check_timer()) { // Check to see if the ping was received.
    cm[currentSensor] = sonar[currentSensor].convert_cm(sonar[currentSensor].ping_result); // Set the sensor distance to the array.
  }
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();
}

I don't have 15 sensors nor an Arduino Mega to totally test this sketch. But, I did test it with 3 sensors and it worked. Let me know how it works with your project. Also, I'd love to see a picture of whatever you have going with 15 sensors.

Tim