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

teckel:

xvjeko:
Hello.. i wrote a code that uses this library, and isn't 100% reliable when debugging...
I'd be grateful if you comment it.

  1. TRIG_PIN_L, TRIG_PIN_R, ECHO_PIN_L and ECHO_PIN_R are never defined.
  2. No reason to set the pin modes, NewPing does that for you.
  3. You shouldn't set the maximum distance to 2000cm on the "NewPing SONAR" lines. The sensor won't work beyond 500cm and setting it to 2000 could make the time between pings from 33ms to 115ms or just cause problems in general. Just remove the ,2000 and it will default to the sensor's maximum distance, which is 500cm and will allow for 33ms between pings as you have it set.
  4. The "constrain" commands will give you problems as out of range results (which are zero) would change to 6. Out of range would typically be beyond 500cm, making it 6cm will probably not give you want you're looking for. Instead, do something like "if (per_l > 0) per_l = constrain(per_l, MIN_DISTANCE, MAX_DISTANCE);" so there can still be a zero result (out of range).
  5. The rest of your code then needs to accommodate what to do with out of range results. It doesn't appear that you have considered that. If anything, a zero result should be changed to MAX_DISTANCE + 1, as that's what it means. Without considering a zero result (out of range), your sketch will probably never work.

That should give you a good start to diagnose what's going on. 1, 3, 4 & 5 are real problems that will seriously cause problems for your sketch.

Tim

thank you sir

EDIT: I got everything to work now... I hope it will run flawlessly in the real life... Thanks again