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

KSWang:
Thank you very much.
Your library is awesome.
However, I want to use with five ultrasonic sensors and one hall sensor.
It is embarrassed if there are any delay time in loop.
Is it mean it will be few delay time/ or no delay if I use the similar way like with calling NewPing five times?

#include <NewPing.h>

NewPing sonar1(11, 12, 200); // Sensor 1: trigger pin, echo pin, maximum distance in cm
NewPing sonar2(9, 10, 200); // Sensor 2: same stuff

#define pingSpeed 100 // Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor
unsigned long pingTimer1, pingTimer2;

void setup() {
// Do other stuff here
pingTimer1 = millis() + pingSpeed; // Sensor 1 fires after 100ms (pingSpeed)
pingTimer2 = pingTimer1 + (pingSpeed / 2); // Sensor 2 fires 50ms later
}

void loop() {
if (millis() >= pingTimer1) {
  pingTimer1 += pingSpeed; // Make sensor 1 fire again 100ms later (pingSpeed)
  int in1 = sonar1.ping_in();
}
if (millis() >= pingTimer2) {
  pingTimer2 = pingTimer1 + (pingSpeed / 2); // Make sensor 2 fire again 50ms after sensor 1 fires
  int in2 = sonar2.ping_in();
  // Both sensors pinged, process results here
}
// Do other stuff here, notice how there's no delays in this sketch, so you have processing cycles to do other things :slight_smile:
}

While there are no delays in that sketch, the ping_in() method does it's own pauses. While it will work with 5 sensors, it will still be using a blocking mode programming paradigm. You would need to use the ping_timer() method to not be using a blocking programming paradigm. I include examples but if you're not an expert on the subject, it will probably be difficult to understand as it's a totally different programming paradigm than you are probably used to using. The above sketch is still basically blocking mode programming.

Tim