New ping library Max Distance

So I have been creating a robot that you can control with your phone. Recently I added a parallax ping sensor to stop the car if it where to get to close to a wall. Well with the new ping library if you exceed your max distance then then the arduino will give you a value of 0. That is a problem because if my car is at a distance grater then 250 centimeters then it will stop its self automatically. So what I want to know is can you change that value you get when you exceed your max distance to a higher value or something like unkown distance?

ping test.ino (304 Bytes)

Post the library (using code tags) or provide a link.

http://playground.arduino.cc/Code/NewPing

When you get a value of '0', why don't you handle it a different way than you're handling it now?

I'm trying to read between the lines. . .like maybe your code says "if distance < 10, stop" and then you get that 0, so you stop, and you want to stop if you get a REAL 0, but you don't want to stop if you get a "MAX DISTANCE 0"?

Is that what's happening?

Have a look in NewPing.h
There are some defines there that you might consider changing but beware of side effects

Alternatively, use the value of 0 to indicate that the car is out of range of any obstruction. Presumably you don't intend to let it get right up to obstructions and return a legitimate zero value.

well here is the line were i specify when i want it to stop. Is their a better when of writing this?

if (distance < 20 )
{
analogWrite (PWMa, 0);
analogWrite (PWMb, 0);
digitalWrite (DIRECTIONa, HIGH);
digitalWrite (DIRECTIONb, HIGH);
}

Yeah that is what i was trying to say

TheCityGame:
When you get a value of '0', why don't you handle it a different way than you're handling it now?

I'm trying to read between the lines. . .like maybe your code says "if distance < 10, stop" and then you get that 0, so you stop, and you want to stop if you get a REAL 0, but you don't want to stop if you get a "MAX DISTANCE 0"?

Is that what's happening?

You know, for starters, you could make your stop something like "if (0 < distance < 20) { //stop}"

Caveat : I'm just saying that from a coding standpoint. If your robot rams into something, don't blame me. Like UKHeli said you really don't care about detecting a real 0 distance. You'll stop before you get there.

TheCityGame:
When you get a value of '0', why don't you handle it a different way than you're handling it now?

I'm trying to read between the lines. . .like maybe your code says "if distance < 10, stop" and then you get that 0, so you stop, and you want to stop if you get a REAL 0, but you don't want to stop if you get a "MAX DISTANCE 0"?

Is that what's happening?

Hey TheCityGame,
I have the same problem and I would like to come up with a better solution without the exclusion of 0 out of the stop threshold. Would you know how to do that?

Got it :slight_smile:

http://forum.arduino.cc/index.php?topic=151329.0

I had the same issue. The place you change it is in this statement:

cm[currentSensor] = 0;

change it to:

cm[currentSensor] = 200;  //or whatever value you choose

It's found here in your sketch:
[/code]

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1)
        oneSensorCycle();                             // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 200;                      // Make distance 200 in case there's no ping echo for this sensor. changed from zero
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }