Go Down

Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.7 (Read 564021 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

harrygover

Hi there,

Apologies if this has been answered already, im a beginner so just trying some small experiments out. When the sensor distance exceeds its maximum it defaults to 0cm. Is there anyway this can be changed to the max distance value?

Thanks

teckel

Hi there,

Apologies if this has been answered already, im a beginner so just trying some small experiments out. When the sensor distance exceeds its maximum it defaults to 0cm. Is there anyway this can be changed to the max distance value?

Thanks
This is a common question/confusion but if you think about it, it makes total sense that it works the way it does.

Imagine you set the max distance to 100cm. Now the way the library works, any non-zero value means there's something in front of the sensor. Let's say the object was 100cm away, it would return a value of 100.

If instead, it would return the max distance when there wasn't an object at all in the way, how would you know there wasn't an object at all? Or if there was an object 100cm away? So there would be no difference between an object detected at 100cm and no object at all. That's not good logic, and it makes it impossible to know what the condition is.

Keep in mind that a result of 0 doesn't mean the object is further away than the max distance. It means there isn't an object AT ALL, which is why a value of 0 makes total sense.

If, however, for your use, you want this ambiguous logic, that's totally possible. Simply read the sensor distance, then if 0 make it any value you want. For example:


Code: [Select]
if (dist == 0) {
 dist = 100;
}


Or whatever you want it to be. Writing the library in this way, you can both detect when an object doesn't exist or just set the max distance (if that makes sense to your project).

Basically, this is where your programming needs to take over and deal with the results as your program needs.

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

zoomx

Zero is much better because you can use a better sensor with longer range so the max distance is not always the same.

Aylee

Hello, thank you very much for your contribution! I have a problem in my project. I have a car, Arduino + L293D shield. I have connected 4x ultrasonic sensors on pins: 3,4,5,6. I have put a delay of 60ms and 5s in setup.

However, the front sensor measures are like 12,12,12,0,12,12,0,12,12... etc a sensor from left measures 0,0,0,0,0,18,0,0,0,18. How could I make a consistent measurment? Some of the sensors are measuring good, some have a lot of zeros.

I need the car to avoid obstacles in its way. Thank you.

teckel

Hello, thank you very much for your contribution! I have a problem in my project. I have a car, Arduino + L293D shield. I have connected 4x ultrasonic sensors on pins: 3,4,5,6. I have put a delay of 60ms and 5s in setup.

However, the front sensor measures are like 12,12,12,0,12,12,0,12,12... etc a sensor from left measures 0,0,0,0,0,18,0,0,0,18. How could I make a consistent measurment? Some of the sensors are measuring good, some have a lot of zeros.

I need the car to avoid obstacles in its way. Thank you.
Not enough information to help.  But, I would first guess that your sensors are not compatible with the one pin method or you didn't install the capacitor (you don't specify the sensor, circuit, nor code, so who knows).

Also, you're over-complicating things if you're having a problem.  SIMPLIFY when you have a problem.  If I were you, I would have just one sensor and use two pins (trigger and echo pins).  See if that works, if it does, see what it takes to make it fail, then you've found your problem.

In any case, it seems you just need some basic deductive reasoning to isolate what the problem is.

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

Aylee

Not enough information to help.  But, I would first guess that your sensors are not compatible with the one pin method or you didn't install the capacitor (you don't specify the sensor, circuit, nor code, so who knows).

Also, you're over-complicating things if you're having a problem.  SIMPLIFY when you have a problem.  If I were you, I would have just one sensor and use two pins (trigger and echo pins).  See if that works, if it does, see what it takes to make it fail, then you've found your problem.

In any case, it seems you just need some basic deductive reasoning to isolate what the problem is.

Tim
Thank you very much for the answer. I didn't used any capacitor on them.
As an idea, i wanted to do something like this: https://youtu.be/I9UEWkUHS2U . He uses hc-sr04 too and it seems to work fine even tough they are 6 sensors, not 4.I have attached the electric scheme, hope you understand something.
All the sonic sensors are connected on the top of l293d shield on +5v and gnd.

Hope you could help me, i need it to get it working fast... thank you for your time.

I would like to connect a capacitor, maybe it helps... what capacitor i need to use?

for coding i used this
Code: [Select]

NewPing sonar[4] = {
  NewPing(3, 3, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE),
  NewPing(5, 5, MAX_DISTANCE),
  NewPing(4, 4, MAX_DISTANCE)
};

SETUP
delay(5000)
serial begin 9600

LOOP
delay(60)
Serial.println(sonar[0].ping_cm());
delay(300); to see what i receive from the sensor. I used println for every sensor.
[code]

teckel

Thank you very much for the answer. I didn't used any capacitor on them.
As an idea, i wanted to do something like this: https://youtu.be/I9UEWkUHS2U . He uses hc-sr04 too and it seems to work fine even tough they are 6 sensors, not 4.I have attached the electric scheme, hope you understand something.
All the sonic sensors are connected on the top of l293d shield on +5v and gnd.

Hope you could help me, i need it to get it working fast... thank you for your time.

I would like to connect a capacitor, maybe it helps... what capacitor i need to use?

for coding i used this
Code: [Select]

NewPing sonar[4] = {
  NewPing(3, 3, MAX_DISTANCE),
  NewPing(6, 6, MAX_DISTANCE),
  NewPing(5, 5, MAX_DISTANCE),
  NewPing(4, 4, MAX_DISTANCE)
};

on setup
serial begin 9600

and

Serial.println(sonar[0].ping_cm());
delay(300); to see what i receive from the sensor. I used println for every sensor.
[code]

Use the KISS rule.  First, use one sensor.  Secondly, connect a trigger and echo sensor to that one sensor.  Run a test to see if that works.  If it works, add stuff back in till it no longer works and that's your problem.  If it still doesn't work, make it even more simple (like use the example sketch only with nothing else).  Still doesn't work, remove the shield, etc.

There's no way anyone can diagnose your problem, as it's not a library problem.  But, it sounds like you're trying to run before you walk.  Walk first, then run.  Trying to do this quickly is basically the problem.

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

Aylee

Use the KISS rule.  First, use one sensor.  Secondly, connect a trigger and echo sensor to that one sensor.  Run a test to see if that works.  If it works, add stuff back in till it no longer works and that's your problem.  If it still doesn't work, make it even more simple (like use the example sketch only with nothing else).  Still doesn't work, remove the shield, etc.

There's no way anyone can diagnose your problem, as it's not a library problem.  But, it sounds like you're trying to run before you walk.  Walk first, then run.  Trying to do this quickly is basically the problem.

Tim
I didn't said it was a library problem but didn't know where to ask. Today I will try what you said, hope it's a l293d shield problem, as i see on the internet, it uses almost all digital pins, if so, i will connect them to analog pins. Is there anyway to stop them and read values one at a time? For example, if i have 4 sensors - 2,3,4 to be OFF and the first sensor to be actived, then 1,3,4 etc

As i can see on other forums, they need to connect to different pins to have a consistent reading. I will come with a replay later this day. thank you!

teckel

I didn't said it was a library problem but didn't know where to ask. Today I will try what you said, hope it's a l293d shield problem, as i see on the internet, it uses almost all digital pins, if so, i will connect them to analog pins. Is there anyway to stop them and read values one at a time? For example, if i have 4 sensors - 2,3,4 to be OFF and the first sensor to be actived, then 1,3,4 etc

As i can see on other forums, they need to connect to different pins to have a consistent reading. I will come with a replay later this day. thank you!
I didn't said it was a library problem but didn't know where to ask. Today I will try what you said, hope it's a l293d shield problem, as i see on the internet, it uses almost all digital pins, if so, i will connect them to analog pins. Is there anyway to stop them and read values one at a time? For example, if i have 4 sensors - 2,3,4 to be OFF and the first sensor to be actived, then 1,3,4 etc

As i can see on other forums, they need to connect to different pins to have a consistent reading. I will come with a replay later this day. thank you!
Analog or digital pins is not the issue. The shield has nothing to do with the sonar sensors, so your confusing things by even including it. And you're still trying to get 4 sensors working before you successfully have one working. Maybe you have 4 bad sensors? No way to tell with the method you're using to build/write this. Strip everything down to just the Arduino, sensor, and 4 wires to it. Make sure it works with the sample sketch. Walk first or you'll never figure out the problem.
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

Aylee

Analog or digital pins is not the issue. The shield has nothing to do with the sonar sensors, so your confusing things by even including it. And you're still trying to get 4 sensors working before you successfully have one working. Maybe you have 4 bad sensors? No way to tell with the method you're using to build/write this. Strip everything down to just the Arduino, sensor, and 4 wires to it. Make sure it works with the sample sketch. Walk first or you'll never figure out the problem.
I'm home now and I've sorted it out, thanks God. The problem was the L293D shield, after removing it the ultrasonic sensors had good reading. I've moved the pins from digital to analog 0-3. Now it's much better:

Code: [Select]

sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 8
sonar0: 19
sonar1: 4
sonar2: 23
sonar3: 7


Many thanks for helping me out.. i tough it was a big problem but it always the little things that complicate us.

navybuff

I am looking for some help building a 2 or 3 sensor unit that produces different tones (audio) as the distance changes.  My brother has had a stroke and is partially blind in his left eye and this creates major issues when walking.  I am new to arduino but have some electronic background.  Any help with sketches would be greatly appreciated...  BTW, great thread and project.  I have a single sensor working now using your NewPing...  Thank you.

MountainBoy

Hello, this is my first post and I have great expectations. https://forum.arduino.cc/Smileys/arduino/smiley.gif

This may not quite be the correct forum so any redirection would be appreciated. Thanks

I have a JSN-SR04 sensor used to measure water depth in a tank. It is connected to a Arduino NANO.
At the present time it is still in the test phase. The system is powered thru the computer USB port. I have measured the power with a voltmeter and everything is correct. Now for the problem if I use the pin triggering and the pulseIn process I receive data.  The problem is that every fifth reading is about 20% lower than the rest. I tried switching to NewPing in hope for better results. Now with NewPing v1.9 I get reading of only zero. Looking at the hardware the green trigger LED on the SR04 board isn't flashing. I would really like to use NewPing whats wrong?

teckel

Hello, this is my first post and I have great expectations. https://forum.arduino.cc/Smileys/arduino/smiley.gif

This may not quite be the correct forum so any redirection would be appreciated. Thanks

I have a JSN-SR04 sensor used to measure water depth in a tank. It is connected to a Arduino NANO.
At the present time it is still in the test phase. The system is powered thru the computer USB port. I have measured the power with a voltmeter and everything is correct. Now for the problem if I use the pin triggering and the pulseIn process I receive data.  The problem is that every fifth reading is about 20% lower than the rest. I tried switching to NewPing in hope for better results. Now with NewPing v1.9 I get reading of only zero. Looking at the hardware the green trigger LED on the SR04 board isn't flashing. I would really like to use NewPing whats wrong?

The sensor may need more current than you're giving it.  How are you powering the sensor?  Maybe use an external power supply instead of USB.
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

Go Up