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

Bajdi:
Has anyone used this library in an obstacle avoiding robot sketch? I have a simple obstacle avoiding sketch that uses an old ultrasonic library, it works. But I wanted to try it with this library. Can I use "Distance = srf06.ping_cm();" in a sketch without the pingtimer/pingspeed code? Or do I have to use the "pingTimer" and "pingSpeed" like in the example? This makes it much harder to use in a sketch. I tried writing a function that returns the distance but I can't get it to work (I'm not much of a programmer).

In the attempt to use IMHO "proper" coding (not using the all too popular dreaded "delay" command), I may have confused some looking for the more typical Arduino code with the typical "delay" commands. I have an alternative agenda with NewPing, to make people think a little outside the box and try to avoid the "delay"s. Anyway, following is maybe an easier to understand sketch that uses old-school "delay" commands with NewPing:

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing srf06(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
}

void loop() {
    // Send out the ping, get the results in centimeters.  Obviously, nothing is being done with this value, your existing sketch would process this information.
    int distance = srf06.ping_cm();

    delay(100);  // Do nothing for 1/10th of a second.  Poor ATmega, it could do 160,000 instructions in this time.
}

There's still a huge advantage in using the NewPing library even using the above sketch. With other ultrasonic libraries (and I assume the one you're using now) if the sensor doesn't get an echo, the Arduino is just sitting there waiting for a full second for the results (which never arrives). During this time, no new processing is happening. A second is a VERY long time, you could be doing 16 million instructions in that time. With the above sketch, the longest it will sit waiting for an echo is about 30ms (because the maximum distance is set to 200cm). Therefore, if you implement NewPing in your existing sketch it should seem much faster and consistent when the sensor is not getting a ping echo. Making your sketch event-driven would further speed up your sketch and allow you to avoid using "delay" commands. But, NewPing doesn't require this, and your sketch will benefit even if you still use delays.

The "ping_timer" and "check_timer" methods along with the resulting "ping_result" value is a totally different way of interfacing with the sensor. This can further make your sketch event-driven which frees up more processing cycles to do other things in your sketch. It may be a little more advanced, but is really not that hard. Here's an ultra-simple example (you would enter your own robot control code as the sketch does nothing as-is).

#include <NewPing.h>

#define TRIGGER_PIN  12 // Arduino pin tied to trigger pin on ping sensor.
#define ECHO_PIN     11 // Arduino pin tied to echo pin on ping sensor.
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). I set this to 50cm because in a robot situation you probably don't care about an object 200cm away.

NewPing srf06(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
    // Set robot to drive forward.
}

void loop() {
    srf06.ping_timer(echoCheck); // Set the function you want to call to check for a ping
    delay(100);  // Do nothing for 1/10th of a second.  Poor ATmega, it could do 160,000 instructions in this time.
}

void echoCheck() { // Timer2 interrupt calls this function every 24uS where you can check the ping status.
      if (srf06.check_timer()) { // This is how you check to see if the ping was received.
          // Detected object within 50cm of robot, call turn function here.  srf06.convert_cm(srf06.ping_result) would be the distance in cm if you need it for your sketch.
      }
}

I hope these scripts make the different methods of NewPing more clear for some.

Tim