Why the Serial monitor file loop many times?

Hi,
This is a example code from:
https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home
The #define ITERATIONS 5 // Number of iterations.,
but the Serial monitor file loops upto 27 times, why?
Thanks
Adam

#include <NewPing.h>

#define ITERATIONS     5 // Number of iterations.
#define TRIGGER_PIN   4 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN      5 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

unsigned long pingTimer[ITERATIONS]; // Holds the times when the next ping should happen for each iteration.
unsigned int cm[ITERATIONS];         // Where the ping distances are stored.
uint8_t currentIteration = 0;        // Keeps track of iteration step.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum 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 = 1; i < ITERATIONS; i++) // Set the starting time for each iteration.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}

void loop() {
  for (uint8_t i = 0; i < ITERATIONS; i++) { // Loop through all the iterations.
    if (millis() >= pingTimer[i]) {          // Is it this iteration's time to ping?
      pingTimer[i] += PING_INTERVAL * ITERATIONS; // Set next time this sensor will be pinged.
      if (i == 0 && currentIteration == ITERATIONS - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar.timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentIteration = i;        // Sensor being accessed.
      cm[currentIteration] = 0;    // Make distance zero in case there's no ping echo for this iteration.
      sonar.ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).

      Serial.print("pingTimer");
      Serial.print(i);
      Serial.print("=");
      Serial.println(pingTimer[i]);
    }

    Serial.print("cm");
    Serial.print(i);
    Serial.print("=");
    Serial.println(cm[currentIteration]);
  }
  // Other code that *DOESN'T* analyze ping results can go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar.check_timer())
    cm[currentIteration] = sonar.ping_result / US_ROUNDTRIP_CM;

  Serial.print("cm60=");
  Serial.println(cm[currentIteration]);
}

void oneSensorCycle() { // All iterations complete, calculate the median.
  unsigned int uS[ITERATIONS];
  uint8_t j, it = ITERATIONS;
  uS[0] = NO_ECHO;
  for (uint8_t i = 0; i < it; i++) { // Loop through iteration results.
    if (cm[i] != NO_ECHO) { // Ping in range, include as part of median.
      if (i > 0) {          // Don't start sort till second ping.
        for (j = i; j > 0 && uS[j - 1] < cm[i]; j--) // Insertion sort loop.
          uS[j] = uS[j - 1];                         // Shift ping array to correct position for sort insertion.
      } else j = 0;         // First ping is sort starting point.
      uS[j] = cm[i];        // Add last ping to array in sorted position.
    } else it--;            // Ping out of range, skip and don't include as part of median.
  }

  Serial.print("cm78=");
  Serial.print(uS[it >> 1]);
  Serial.println("cm");
  delay(2);
}

SERIAL MONITOR FILE:

cm1=0
cm2=0
cm3=0
cm4=0  ///////////////////// 1
cm0=0
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 2
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 3
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 4
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 5
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 6
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 7
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 8
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0  ///////////////////// 9
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 10
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 11
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 12
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 13
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 14
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 15
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 16
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 17
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 18
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 19
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 20
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 21
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 22
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 23
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 24
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 25
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 26
cm1=0
cm2=0
cm3=0
cm4=0
cm0=0 ///////////////////// 27
cm1=0
cm2=0
cm3=0
cm4=0
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer0=240
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer1=273
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer2=306
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer3=339
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer4=372
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm78=
26cm
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=15
pingTimer0=405
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm60=0
cm60=0
cm60=0
cm60=27
pingTimer1=438
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm60=0
cm60=0
cm60=0
cm60=27
pingTimer2=471
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer3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1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm78=
15cm
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=15
pingTimer0=735
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm60=0
cm60=0
cm60=0
cm60=27
pingTimer1=768
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer2=801
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer3=834
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer4=867
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm78=
26cm
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=33
pingTimer0=900
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm1=33
cm2=33
cm3=33
cm4=33
cm0=33
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer1=933
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
c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m4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm0=27
cm1=27
cm2=27
cm3=27
cm4=27
cm78=
26cm
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=15
pingTimer0=1065
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer1=1098
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer2=1131
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm60=0
cm60=0
cm60=0
cm60=26
pingTimer3=1164
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm60=0
cm60=0
cm60=0
cm60=8
pingTimer4=1197
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm0=8
cm1=8
cm2=8
cm3=8
cm4=8
cm78=
8cm
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=15
pingTimer0=1230
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2=15
cm3=15
cm4=15
cm0=15
cm1=15
cm2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=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm0=26
cm1=26
cm2=26
cm3=26
cm4=26
cm78=
15cm
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=0
cm60=33

Because the code in loop() runs asynchronously, not just once each time through loop(). So loop() may complete many times without doing anything because not all the pings are accounted for.

1 Like

It looks like you are trying to take distance samples every 75 milliseconds and find the median of the last five valid echoes.

I think the method you are using (a separate timer for each array entry) is more complicated than necessary. What I would do is a single 75 millisecond timer:

void loop()
{
  static unsigned long timerStart;
  if (millis() - timerStart >= 75)
  {
    timerStart += 75;
    AddAMeasurement();
  }
}

void AddAMeasurement()
{
    int distance_cm = sonar.getDistance_cm();
    if (distance_cm > 0) // Valid echo
      AddDistanceToMedian(distance_cm);
}

void AddDistanceToMedian(int cm)
{
  // Remove the oldest sample and 
  // insert the new sample in sorted order
}

int GetMedian()
{
  return cm[2]; // Middle of 5 sorted samples
}
1 Like

Thank you.
you did simplify the code and given a batter idea,