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

There’s no reason to put people down here, especially if they are being kind. I didn’t mean your code doesn’t work, I meant (as you stated) it probably needs median assistance. I know the limitations of sonar and I’m not putting it at a 90 degree when testing nor am I using soft materials. I’m actually using a flat board just like you stated.
I already had code similar to this running, using your GitHub, but I cannot get the median to work with multiple sensors. You have many fans of your code asking for an example using median and multiple sensors. Any way you can put that up?
I really do appreciate your support, I think it’s really cool you continue to support this community as I’m a teacher.
Thank you!

tsmacinspires: There's no reason to put people down here, especially if they are being kind. I didn't mean your code doesn't work, I meant (as you stated) it probably needs median assistance. I know the limitations of sonar and I'm not putting it at a 90 degree when testing nor am I using soft materials. I'm actually using a flat board just like you stated. I already had code similar to this running, using your GitHub, but I cannot get the median to work with multiple sensors. You have many fans of your code asking for an example using median and multiple sensors. Any way you can put that up? I really do appreciate your support, I think it's really cool you continue to support this community as I'm a teacher. Thank you!

As for working with multiple sensors, that example has already been written, and is on the official page. Please see the example "Ping 3 Sensors Sketch". Start with that code, not code using a programming paradigm you don't understand. See the NewPing official page that includes all kinds of warnings NOT to use code that you don't understand (which is what you're using now and why you're having problems).

Next, there's already a method for doing multiple pings and returning the median result called ping_median(). In the "Ping 3 Sensors Sketch", you would simply exchange

ping_cm()

with

ping_median() / US_ROUNDTRIP_CM

That's it! That's why I say it's uber simple, just use ping_median()! It's already all written for you, why make it difficult by instead using code you don't understand?

As I've said probably 100 times in this forum, don't use code examples you don't understand. Use the examples on the official page and HEED ALL WARNINGS about NOT using an event-driven programming paradigm example if your not an expert.

If you feel you're and expert coder or you must use the event-driven 15 sensor sketch example instead of the 3 sensor example, there's a HUGE wiki with tips (and a TON or warnings that it's probably over your head). The wiki is (again) on the official site, here's the direct link:

Help with 15 Sensors Example Sketch

So what's this nonsense about people wanting to use median results with multiple sensors? The code is all written for you, just use it!

Can you understand my frustration? I've written the library, I've given you the examples, and instead of using these, you choose to use the most complex example that's beyond your understanding, ignore all the warnings, refuse to read the wiki, wonder why it's not working, then ask for your code to be debugged, when it will never work as you're using two different programming paradigms in the same code. It will NEVER work! There's no way to fix it. Start over using code examples you understand (simple blocking mode programming paradigm).

I've literally said this 100 times. From now on, anyone who asks for help when using code they obviously don't understand will be pointed to the wiki and that's it. If you're an expert coder, that's all you'll need. If you're not an expert, not even God can help you.

I'm sorry for your frustration. Once again, I only went to the convoluted code after trying the code on your GitHub. I tried your instructions to change ping_cm() with Code: [Select] ping_median() / US_ROUNDTRIP_CM and it didn't work for me. Then I went to this forum and found that code you hate. I will give it a shot again. Thanks.

Hello, I am testing your library multi sensor example with 5 sensors on arduino mega every thing is work just fine i am not using any delay in my sketch using 15 sensors unmodified (removed only serial.print) my sketch should run on 250HZ constant loop time and my sketch keep 250HZ confirmed with oscilloscope but when arrive ping time the loop time goes to 240HZ then come back again. is there a way to make this library true multitask to solve my issue?? thanks in advance for any help.

No body has Answer my question so answering myself. newping library is not as multitask as claimed and vary cycle time that cannot be tolerate in a serious robot project i have solved my problem by adding an Arduino Nano as driver for 5 sr-f06 and connected with Arduino Mega via I2C so every variation of time remain in Nano and Mega have perfect timing for drive motors. Anyway Thanks to Author of this library.

For generating 4 Milliseconds Pulse for BLDC

Hi, Silly question but does anyone know how to store the actual sensor data into 3 separate variables (or array) from the 3 sensor example so that they can be recalled later in the code if needed?

@jaybelfast

Other post/duplicate DELETED Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum. It will help you get the best out of the forum in the future. Other general help and troubleshooting advice can be found here.

jaybelfast:
Hi, Silly question but does anyone know how to store the actual sensor data into 3 separate variables (or array) from the 3 sensor example so that they can be recalled later in the code if needed?

Sounds like you need to do some self-education.

Start with the basic example sketches and also take time to study the section of the on-line reference that talks about variables (which includes arrays): https://www.arduino.cc/reference/en ← can also be accessed from the “RESOURCES” tab of the Arduino website menu.

@teckel Via MySensors I learned about your library NewPing. In one of their examples with the HC-SR04 they are using your library. I did build the sensor and it was working great. But at regular times, I did receive a 0 as measured distance. Thought first that it was a hardware problem, but than I saw the remark in the example:

Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

When digging into the library source code I saw that NO_ECHO is returned in case of a problem. Default is NO_ECHO equal to 0

unsigned long NewPing::ping_cm(unsigned int max_cm_distance) {
    unsigned long echoTime = NewPing::ping(max_cm_distance); // Calls the ping method and returns with the ping echo distance in uS.
#if ROUNDING_ENABLED == false
    return (echoTime / US_ROUNDTRIP_CM);              // Call the ping method and returns the distance in centimeters (no rounding).
#else
    return NewPingConvert(echoTime, US_ROUNDTRIP_CM); // Convert uS to centimeters.
#endif
}

So the method ping_cm is returning 0 if there is a problem, but 0 is also a valid distance, maybe a strange one because that would say that something is just before the sensor...

So why not chosen -1 as returning value for NO_ECHO in case of a problem? A distance of -1 is not possible, so people are directly put on the right track of an error...

But maybe there is a specific reason for choosing NO_ECHO equal to 0?

evb: @teckel Via MySensors I learned about your library NewPing. In one of their examples with the HC-SR04 they are using your library. I did build the sensor and it was working great. But at regular times, I did receive a 0 as measured distance. Thought first that it was a hardware problem, but than I saw the remark in the example:

Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

When digging into the library source code I saw that NO_ECHO is returned in case of a problem. Default is NO_ECHO equal to 0

unsigned long NewPing::ping_cm(unsigned int max_cm_distance) {
    unsigned long echoTime = NewPing::ping(max_cm_distance); // Calls the ping method and returns with the ping echo distance in uS.
#if ROUNDING_ENABLED == false
    return (echoTime / US_ROUNDTRIP_CM);              // Call the ping method and returns the distance in centimeters (no rounding).
#else
    return NewPingConvert(echoTime, US_ROUNDTRIP_CM); // Convert uS to centimeters.
#endif
}

So the method ping_cm is returning 0 if there is a problem, but 0 is also a valid distance, maybe a strange one because that would say that something is just before the sensor...

So why not chosen -1 as returning value for NO_ECHO in case of a problem? A distance of -1 is not possible, so people are directly put on the right track of an error...

But maybe there is a specific reason for choosing NO_ECHO equal to 0?

It's not a negative because the method returns an unsigned int. And zero is fine as it will always return at least 1 if the ping is successful. There's no such situation if the distance is so close that it's less than 1, you're thinking about it wrong.

Basically, your obsessing over something when there isn't a problem. And if you want NO_ECHO to be something different for some silly reason, just change it. Or, make a condition on your end to deal with a no echo situation. And when creating that condition, does it really matter if it's 0 or -1 if it always means no echo?

Basically, what you're suggesting would be slower, use more memory, and not provide any benefit. That's why it's zero. I've been doing this a long time...

Tim

asghar144: No body has Answer my question so answering myself. newping library is not as multitask as claimed and vary cycle time that cannot be tolerate in a serious robot project i have solved my problem by adding an Arduino Nano as driver for 5 sr-f06 and connected with Arduino Mega via I2C so every variation of time remain in Nano and Mega have perfect timing for drive motors. Anyway Thanks to Author of this library.

It is if you know what you're doing. And how do you expect anyone to help if you don't include your code? It sounds like your issue was a timer conflict, but just a guess without your code.

Tim

Hello! I’m working on a project using JSN-SR04T with Arduino Uno WiFi Rev2. I stumbled upon an error compiling while uploading the sketch into the arduino. Any clue how to fix this? Thank you.

Sketch .imo: Arduino/JSN-SR04T.ino at master · jumejume1/Arduino · GitHub
Tried using the latest NewPing library v1.9.1

Error message:
----------------------Start of Error Message-------------------------------------------------------

Arduino: 1.8.12 (Windows 10), Board: “Arduino Uno WiFi Rev2, None (ATMEGA4809)”

WARNING: library NewPing claims to run on avr, arm architecture(s) and may be incompatible with your current board which runs on megaavr architecture(s).
C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp: In static member function ‘static void NewPing::timer_us(unsigned int, void (*)())’:

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:254:3: error: ‘OCR2A’ was not declared in this scope

OCR2A = min((frequency>>2) - 1, 255); // Every count is 4uS, so divide by 4 (bitwise shift right 2) subtract one, then make sure we don’t go over 255 limit.

^~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:255:3: error: ‘TIMSK2’ was not declared in this scope

TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:255:3: note: suggested alternative: ‘TIMERB2’

TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.

^~~~~~

TIMERB2

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:255:17: error: ‘OCIE2A’ was not declared in this scope

TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp: In static member function ‘static void NewPing::timer_ms(long unsigned int, void (*)())’:

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:274:3: error: ‘OCR2A’ was not declared in this scope

OCR2A = 249; // Every count is 4uS, so 1ms = 250 counts - 1.

^~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:275:3: error: ‘TIMSK2’ was not declared in this scope

TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:275:3: note: suggested alternative: ‘TIMERB2’

TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.

^~~~~~

TIMERB2

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:275:17: error: ‘OCIE2A’ was not declared in this scope

TIMSK2 |= (1<<OCIE2A); // Enable Timer2 interrupt.

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp: In static member function ‘static void NewPing::timer_stop()’:

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:286:3: error: ‘TIMSK2’ was not declared in this scope

TIMSK2 &= ~(1<<OCIE2A);

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:286:3: note: suggested alternative: ‘TIMERB2’

TIMSK2 &= ~(1<<OCIE2A);

^~~~~~

TIMERB2

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:286:18: error: ‘OCIE2A’ was not declared in this scope

TIMSK2 &= ~(1<<OCIE2A);

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp: In static member function ‘static void NewPing::timer_setup()’:

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:311:3: error: ‘ASSR’ was not declared in this scope

ASSR &= ~(1<<AS2); // Set clock, not pin.

^~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:311:3: note: suggested alternative: ‘SS’

ASSR &= ~(1<<AS2); // Set clock, not pin.

^~~~

SS

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:311:16: error: ‘AS2’ was not declared in this scope

ASSR &= ~(1<<AS2); // Set clock, not pin.

^~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:311:16: note: suggested alternative: ‘A2’

ASSR &= ~(1<<AS2); // Set clock, not pin.

^~~

A2

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:312:3: error: ‘TCCR2A’ was not declared in this scope

TCCR2A = (1<<WGM21); // Set Timer2 to CTC mode.

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:312:3: note: suggested alternative: ‘TCB2’

TCCR2A = (1<<WGM21); // Set Timer2 to CTC mode.

^~~~~~

TCB2

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:312:16: error: ‘WGM21’ was not declared in this scope

TCCR2A = (1<<WGM21); // Set Timer2 to CTC mode.

^~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:313:3: error: ‘TCCR2B’ was not declared in this scope

TCCR2B = (1<<CS22); // Set Timer2 prescaler to 64 (4uS/count, 4uS-1020uS range).

^~~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:313:3: note: suggested alternative: ‘TCB2’

TCCR2B = (1<<CS22); // Set Timer2 prescaler to 64 (4uS/count, 4uS-1020uS range).

^~~~~~

TCB2

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:313:16: error: ‘CS22’ was not declared in this scope

TCCR2B = (1<<CS22); // Set Timer2 prescaler to 64 (4uS/count, 4uS-1020uS range).

^~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:314:3: error: ‘TCNT2’ was not declared in this scope

TCNT2 = 0; // Reset Timer2 counter.

^~~~~

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:314:3: note: suggested alternative: ‘TCB2’

TCNT2 = 0; // Reset Timer2 counter.

^~~~~

TCB2

In file included from C:\Users\Tengku M. Qayyum\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.6\cores\arduino/Arduino.h:27:0,

from C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.h:148,

from C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:7:

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp: In function ‘void TIMER2_COMPA_vect()’:

C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing\src\NewPing.cpp:337:6: warning: ‘TIMER2_COMPA_vect’ appears to be a misspelled ‘signal’ handler, missing ‘__vector’ prefix [-Wmisspelled-isr]

ISR(TIMER2_COMPA_vect) {

^

Multiple libraries were found for “NewPing.h”
Used: C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\NewPing
Not used: C:\Users\Tengku M. Qayyum\Documents\Arduino\libraries\teckel12-arduino-new-ping-2ebf391d9be2
exit status 1
Error compiling for board Arduino Uno WiFi Rev2.

---------------End of Error Message--------------------------------------------------------------

Did you notice this?WARNING: library NewPing claims to run on avr, arm architecture(s) and may be incompatible with your current board which runs on megaavr architecture(s).

TheMemberFormerlyKnownAsAWOL: Did you notice this?WARNING: library NewPing claims to run on avr, arm architecture(s) and may be incompatible with your current board which runs on megaavr architecture(s).

I was about to say basically the same thing.

Tim

Hello, I am looking for a way to use 4 sensors with this code. Which it superbly does. It’s an amazing piece of code. But I am looking to average the readings from the 4 sensors. My problem is the last bit of code

for (uint8_t i = 0; i < SONAR_NUM; i++) {
Serial.print(i);
Serial.print("=");
Serial.print(cm*);*

  • Serial.print("cm ");*
  • }*
    Just pushes the readings in four separate ways. How can I take the readings from the 4 sensors and get their average and put out on the screen?
    Thank you.

Add them all together and divide by four.

Please remember to use code tags when posting code

What sense does it make for return type of ping_median() to be unsigned long when the array uS[it] is of type unsigned int?

NewPing library does not work on ESP32

TheMemberFormerlyKnownAsAWOL: Add them all together and divide by four.

Please remember to use code tags when posting code

Yes..But how?

worked with 2 sensors. Tried adding cm[0] and cm[1] together but the values I get are bigger. For example if the sensor 1 is 9cm so cm[0] should also be 9cm but it is 90cm.

I created a new array "fyp' and used memcpy to copy the cm array into it but only the cm[0] gets copied to fpy[0] and fyp[1] is 0cm even when cm[1] gives 10cm