Robot falling over edges

Hi. im working on an autonomous firefighting robot. im facing challenges with my edge detection code. im using hcsr04 sensors. One sensor is facing ahead to detect obstacles in front of the robot,this one is working good, the other is facing downwards to sense edges. The robot is supposed to move back and change direction when it senses an edge, but the problems is it moves back after it has already fallen. can you assist with code that makes the robot reverse and turn once an edge is detected.
THANK YOU

test.ino (2.88 KB)

#define TRIG_PIN 23
#define ECHO_PIN 22
#define TRIG_PIN2 24
#define ECHO_PIN2 25
#define TRIG_PIN3 12
#define ECHO_PIN3 11
#define MAX_DISTANCE 50
#define COLL_DIST 30 
#define FALL_DIST 30 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
NewPing sonar2(TRIG_PIN2, ECHO_PIN2, MAX_DISTANCE);
NewPing sonar3(TRIG_PIN3, ECHO_PIN3, MAX_DISTANCE);

Numbering 2 out of 3 names in a "set" looks silly. Number them all, or don't number any of them (of course, that means use an array).

int readPing() {
  delay(70);
  unsigned int uS = sonar.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  return cm;
}
int readPing2() { 
  delay(70);
  unsigned int uS = sonar2.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  return cm;
}
int readPing3() { 
  delay(70);
  unsigned int uS = sonar3.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  return cm;
}

Three functions that do EXACTLY the same thing is a waste.
Three functions that do EXACTLY the same thing is a waste.
Three functions that do EXACTLY the same thing is a waste.

Why are there delay()s in loop()? Why are there delay()s in the readPing() functions?

      loop();

NEVER!

You should NEVER have a recursive call to loop().

My advice would be as a first step, step it forward slowly and make sure the values you are getting from your transducers when it gets close to the edge are values you can use to successfully detect the edges in time.

Printouts give you a lot of information telling you what is actually going on, rather than what you think is going on. Put in lots of prints to get the information you need to debug the problem.

I would read all the ping sensors at once (at the start of loop() ) so that I have all the position data at once.

Then I would check the values and decide what I need to do.

Doing it like this would also allow you to print all the values so you could check that the readings are what you expect.

My loop() would probably look like this

void loop() {
   readPingSensors();
   readOtherSensors();
   doOtherStuff();
   moveRobot();
}

Keep in mind that loop() should be repeating 100s or 1000s of times per second.

...R

Kinda off-topic - but I was looking at the NewPing library docs here:

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

...and I noticed something (which I had noticed in OP's code) - with the NewPing library, you don't need a delay; NewPing allows you to ping and then immediately continue with your loop. This is done using a combination of the NewPing methods ping_timer() and check_timer().

The first example in the documentation shows what you are currently using, with adding a delay() to your loop. Instead - study how the second example works, which is set up to ping and return results rapidly from multiple sensors (15 sensors in the example).

You have only a couple - so you might be able to simplify your code; using arrays as in the example might be overkill in your case. Or - if you think (or know) you will be adding more ultrasonic sensors, keep the array so you can expand your sensor network.

Essentially, the way the NewPing example shows (and that NewPing allows for) is a form of the "blink without delay" example applied to multiple ultrasonic sensors. Study "blink without delay" (as well as it's sister example - Demonstration Code for Several Things at the Same Time) - they are both very important things to understand, especially for use in robotics projects where you can't afford any blocking code.

edit: adding link tags