Go Down

Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7 (Read 552918 times) previous topic - next topic

teckel

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 <Two sensor example> with calling NewPing five times?

Code: [Select]

#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 :)
}



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
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

developerunify

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 <Two sensor example> with calling NewPing five times?

Few months ago I had a similar requirement of using 4 ultrasonic sensors together attached to 4 corners. Instead of killing time hassling and fine tuning 4 separate sensors I had bought a board which had 4 transducers/probe with 1 board. I believe it had UART output of distances so not much calculation code required on the micro-controller side.

Edit: Found out the item I had bought couple months ago. It was something like this https://www.ebay.com/itm/254197614525. Also here is another model which showed up in the search results serving the same purpose: https://www.ebay.com/itm/254203570456

Go Up