Go Down

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

teckel

Hi, i'm trying to use the NewPing library to have 8 sensors(HC-SR04) working together
It's a parking lot with 8 places, each one has a sensor but i'm having an issue, I can only put 4 sensors to work, when I use the 5, the first one stops working

I found a code online that uses only 2, and tried to make it to work with the 8 sensors I need

I can't seem to find the problem, would be glad if someone could help me, it's a project for school, and need it as soon as possible

The code attached is just about the 8 parking sensors, I still need to add 2 of them for the gate, to use with 2 servos on the entrance and exit

Thank you


How are you powering these 8 sensors?  They probably need more power than the Arduino can output.  To me, it sounds like a physical problem, not a software problem.  You should be powering the sensors with an external power source.

Also, as I've tried to mention maybe 100 times, if you don't understand an event-driven programming paradigm, DON'T use my 15 sensor sketch as a code base.  By how you're using all individual variables instead of arrays and loops for 8 sets of RGB lights, it doesn't seem that you have a firm grasp of better ways of implementing your code and therefore the event-driven 15 sensor sketch may not be the best code base for your project.

Not sure how many dozens of times I've stated this, but PLEASE use my 3 sensor sketch example if you're new to programming (this can be scaled up to any number of sensors): 3 Sensor Sketch

If you feel you understand the event-driven programming paradigm that's built into the 15 sensor sketch, I've created a LONG document on how it all works: Help with 15 Sensors Example Sketch

Please use those resources and you should be well on your way.

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

dynamo

Hi! i need to know which functions are contained in newping library. Could it be possible? Thanks!

teckel

Hi! i need to know which functions are contained in newping library. Could it be possible? Thanks!
See the first post in this thread with the large link which reads "NewPing Library Project Homepage" that's will show all the methods and a bunch of other info. Heck, you could just do a Google search for "NewPing" and get what you're looking for. Maybe I'm missing something?

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

kaluf

Hi there. I am relatively new to c++ programming and have been trying to get median value of two sensors together using your 3 sensor sketch. I have tried using the RunningMedian library without success. It is also worth mentioning that i have stumbled upon this topic where you describe a possible solution to the issue that i am having without the actual implementation.

 If you or someone could just point me in the right direction to a simple solution to this problem that would be great. Thank you for a very useful library.

Btw here is my broken code:
Code: [Select]
// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------

#include <NewPing.h>
#include "RunningMedian.h"

RunningMedian samples = RunningMedian(32);

#define SONAR_NUM 2     // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.


NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(11, 12, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(6, 7, MAX_DISTANCE),
};

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  float median;
 
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    samples.add(sonar[i]);
    median = samples.getMedian();
    Serial.print(median);
  }
  Serial.println();
}


edit:
this code seems to be working but i am not completely sure and i would like to use the the millisecond values instead of cm anyway..
Code: [Select]
// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------

#include <NewPing.h>
#include "RunningMedian.h"

RunningMedian samples = RunningMedian(32);

#define SONAR_NUM 2     // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.


NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(11, 12, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(6, 7, MAX_DISTANCE),
};

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  float median;
 
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    samples.add(sonar[i].ping_cm());
    median = samples.getMedian();
    Serial.print(median);
  }
  Serial.println();
}

teckel

I have tried using the RunningMedian library without success. It is also worth mentioning that i have stumbled upon this topic where you describe a possible solution to the issue that i am having without the actual implementation.
The problem is that you have a specific use that is outside of the actual NewPing library.  I could write your program for you, but what fun would that be?  As I have no idea why *YOU* want to do with your project, writing the specific code that you need for you would be way outside the scope of the NewPing library.

Anyway, that link does show how the median method works, you would just need to refactor it for your needs.  The heavy lifting is already done for you, you just need to put lipstick on it for your specific needs.  Basically, I've done 95% for you, you only need to implement it in your code for your specific needs.

this code seems to be working but i am not completely sure and i would like to use the the millisecond values instead of cm anyway..
Then use the ping() method instead of the ping_cm() method.  Also, while it may "work" using that library is a terrible idea IMHO.  Refactoring my ping_median() method would be much better.

Also, you haven't specified what you're trying to do.  Are these two sensors pointing the same direction?  Are you just trying to get some kind of average between two sensors or in different directions.  Basically, if we had a better idea of what your project was, there could be a totally different solution that would work better.

For example, maybe you should be using the ping_median() function with NewPing for the two sensors and then just add the two values together and divide by 2?  That would be better at resolving the "noise".  But, without knowing the details your project, I can only speculate that this would be a better solution.

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

dynamo

Hi again!. I have another problem with my recently program. The program works perfectly, but after minutes of ejecution all result are zero, why? could it be the WatchDog??? i mean, no more pings, no more results, no more measurement.


drphil

Hi, I just had a couple of questions.

If I want to use the one-pin method, do I need a resistor between the 2 pins, I read elsewhere that people were doing that.

Second question: How do I get it to display/record beyond the nearest cm? I see that rounding is supposed to be off by default, but my measured distances always come out to the nearest cm.

teckel

Hi, I just had a couple of questions.

If I want to use the one-pin method, do I need a resistor between the 2 pins, I read elsewhere that people were doing that.

Second question: How do I get it to display/record beyond the nearest cm? I see that rounding is supposed to be off by default, but my measured distances always come out to the nearest cm.
Only some sensors would need the capacitor.  It's a capacitor, not a resistor.  A resistor would be a BAD idea!  This is all spelled out at the top of example single pin sketch, see the comments at the top of the sketch:

https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home#!single-pin-sketch

You're confusing rounding and truncating.  If you're using the ping_cm() method, you'll always get a result that's in integers.  The difference is if it truncates (default) or rounds.  The censors are not accurate to fractions of a CM, and using floating point math greatly slows down the Arduino and uses far more programming space.  So, all the math in NewPing is with integers.  If you really want to do a fraction of a CM (which won't be anymore accurate anyway) you need to use the ping() method and then apply your own floating point math to convert the speed of sound to fractions of a CM.

This isn't done in the library as it would be pointless and make it slower and use more memory.  But, if you really want to waste speed and memory, by all means do the floating point math.  Be ready to find out there's a debate on the speed of sound, so that's another margin of error thrown in.  Then, be ready to add a temperature sensor and add that value to your computation as that's also part of the equation for the speed of sound.  Or, just use the ping_cm() method that's built-in, faster, uses less memory, and is just as accurate in the end.  Your choice...

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

teckel

which timer does this library use? I just want to confirm that it won't interfere with timer 1
Well documented on the official NewPing site.  Where are you looking?

https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

Also, the answer is "it depends", because it can use no timers.

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

nityoday

Can someone please help me with the code ? Please ? I want that a buzzer should be activated when distance is less than 50, with NewPing. The if function . Please help .

teckel

Can someone please help me with the code ? Please ? I want that a buzzer should be activated when distance is less than 50, with NewPing. The if function . Please help .
If you're asking for a programmer to contract to write you a program, maybe try a local college.  There's probably a student who would write this for you and build the circuit for not much money.  If you're asking here to get it for free, what's the point?  All the fun is designing the circuit and writing the program.  We wouldn't want you to miss out on all the fun!

But in general, there's really not much to write.  You can easily use my example code to get a distance in CM.  Then a simple

Code: [Select]
if (cm < 50) {
 digitalWrite(D1, HIGH);
} else {
 digitalWrite(D2, LOW);
}


would do it.  If this is beyond you, I'd suggest learning to program (and this would be a VERY simple first program), or using the above suggestion of finding a student going to college that would be build it for you for probably just a few bucks (college kids always need beer money).

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

teckel

I'm looking here
So are you specifically talking about using NewPing with the 15 sensor sketch example?  That's something new you didn't mention in your first post, something that TOTALLY changes the answer too!

First of all, you should read the warning at the top of the 15 sensor sketch.  This is where I try to talk you out of using it.  Basically, if you don't know what it's doing, and you don't know what timer it's using, it's probably outside your pay grade and maybe you should stick with something simple and easy first.  If you're read the warning, and fully understand the programming paradigm in the 15 sensor sketch, then you should already know the answer to your question!

If you need some help with the 15 sensor sketch, maybe you should try the link in the index of that page which is labeled "Help with 15 Sensors Sketch".  That would seem the obvious place you would go if you wanted help with the 15 sensor sketch, but maybe I'm just too close to it to understand the confusion.

Also, please heed the warning that I put in bold for the 15 sensor sketch:

This sketch is designed for seasoned programming experts, don't use unless you really know what you're doing!

Basically, no assistance will be given, because if you're a seasoned programmer you don't need to ask, and if you're not a seasoned programmer, you shouldn't be using it.

Also, if you read the entire page (from the top) that you posted, you'll have your answer.  May I suggest doing a search for "timer" on that page to see if the very page you were on when you had a question was discussed (hint, "timer" is mentioned 83 times on the page that you referenced).

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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

TeslaIaint


yugro

Hello Tim:

We are using a HC-SR04 to measure the water level in a nutrients tank.

Tank is 12.75in diameter with open top.
Depth of the tank is 15in

The HC-SR04 is installed at the top in the center of the tank
There is no obstruction of the sensor beam on 11 in diameter from the center.
Then, we have a few 3/8" poly tubes on the periphery of the tank.

Currently, we get maximum distance variation measurement of about 2" for the reading (Usually correct distance to 2" less)

I have attached the code we are using.

From your experience, can we get more stable readings from the sensor?

Thank you in advance.

Best regards,
Serge

teckel

you're welcome for the penny. Don't get too drunk
Very funny!  I wondered who would do that.  Made me laugh, thanks!  Although, PayPal got your entire penny :(

Tim
My hardware: Arduino, Teensy 2.0, Teensy 3.2 & custom 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