Suggest change to default NO_ECHO precompiler value in NewPing.h

Issue #57 closed
Dave Eslinger created an issue

The current default value for NO_ECHO is set to 0. This results in a condition where, when the field in front of the sensor is completely clear, it returns a distance of zero. That makes it awkward to use logic similar to “If distance > 5, do something.” Instead, a user must also check for a distance of 0, which, actually, could be a valid measured distance. I suggest NO_ECHO be changed to 28500, which is the value of US_ROUNDTRIP_CM * MAX_SENSOR_DISTANCE. This would result in a non-detect of an echo giving the maximum value that is being tested for.

This change would occur on line 173. Here is the version in my copy of NewPing.h:

    #define NO_ECHO 28500    //DLE changed to US_ROUNDTRIP_CM * MAX_SENSOR_DISTANCE; Value returned if there's no ping echo within the specified MAX_SENSOR_DISTANCE or max_cm_distance.

Comments (5)

  1. Tim Eckel repo owner

    The problem is, that indicates there IS an object at that distance, which could cause a problem for a different project. You’re only considering your own situation, not others.

    You should either just change the NO_ECHO value (which is why it exists) or return the echo value and insert a condition (like if 0 then set to whatever).

    What you have to keep in mind is that no echo could mean all kinds of different things and should be treated all kinds of different ways based on your project. You need to programmatically deal with this uniqueness for your project.

    Hope this clears things up,

    Tim

  2. Dave Eslinger reporter

    Hi Tim,

    Thanks for the quick response and for the amazing NewPing package. It really does a great job. As you mention I can (and do) change the NO_ECHO value in my personal library (changing in source code with precompiler directives doesn’t seem to work, alas.) I just opened the issue to make sure you are aware of the behavior, which you clearly are. An additional reason I felt it worth mentioning is that it took a while for me to track this down as the cause of the apparently “random” zeros students would get in a robotics course. I made an assumption (with all that entails) that a non-return would give the maximum distance value, which one can specify. I may have missed this behavior in the documentation, but I just got it originally from the Arduino site, so there wasn’t too much. But now, others can at least find it mentioned in an issue, I suppose. In any case, thanks again for the awesome package and for keeping it going. It does make using these small ultrasonic sensor much easier than some of the other options. Your work is greatly appreciated.

    Cheers,

    Dave

  3. Former user Account Deleted

    Yeah, there’s two trains of thought with this. One is that nothing in “view” means it could be clear, and the other would be that it’s clear up to a certain point. The problem with sending the max ping distance, is that could mean there’s something at that distance when there’s not, which could cause programic adjustments when not needed. This could make decisions which are undesirable for certain projects. Instead, a zero value is returned when there’s no ping within range. Meaning there may be nothing, or at least nothing within the range.

    This has been a point of confusion since the beginning, but it really depends on the project, and the zero result gives an absolute value that you can use to make the calculations based on your project.

  4. Log in to comment