Problem with my PING code?

Hi, I am trying to get a robot to go forward until an object is detected within 5 inches. Then I want it to go backwards 10 inches from the object, then I want it to turn right. And finally, repeat the loop by going forward again and checking for an object within 5 inches. My robot just isn’t moving in the correct manner. Is there an issue with my code?

Here is my code:

#include <AFMotor.h>

 
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_1KHZ);
AF_DCMotor motor4(4, MOTOR12_1KHZ);
int ultraSoundSignal = 19;     // assigning the PING sensor to Digitial Pin 19
unsigned long ultrasoundValue = 0;
unsigned long echo = 0;


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(ultraSoundSignal,OUTPUT);
  motor1.setSpeed(200);     
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);
}

unsigned long ping(){

 pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
 digitalWrite(ultraSoundSignal, LOW); // Send low pulse
 delayMicroseconds(2); // Wait for 2 microseconds
 digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
 delayMicroseconds(5); // Wait for 5 microseconds
 digitalWrite(ultraSoundSignal, LOW); // Holdoff
 pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
 digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
 echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
 ultrasoundValue = (echo / 58.138) * .39; //convert to CM then to inches
 return ultrasoundValue;
 }

void forward()     // the forward function
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(FORWARD);
  motor4.run(FORWARD);
}

void backward()     // the backward function
{
  motor1.run(BACKWARD);
  motor2.run(BACKWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

void haltMotors()  //the functon to stop the motors before reversing direction
{
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}

void turnRight()   // the function to turn right
{
  motor1.run(FORWARD);
  motor2.run(FORWARD);
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
}

void loop() {
  ping();
  while (ultrasoundValue > 5)  //while robot is 5 inches away from object
    {
      forward();       //go forward
      ping();            // update position
    }
  haltMotors();   //stop the motors before reversing direction
  delay(3000);
  while (ultrasoundValue < 10)  //move robot back 10 inches before turning
   {
     backward();    
     ping();                 //update position
    }
  turnRight();
  delay(5000);
}

My robot just isn't moving in the correct manner

It's dancing the cha-cha?

Why don't you tell us what it is doing, instead of what it is not doing?

Some debug prints might point you in the right direction (no pun intended)

Your ping function returns a value - you may want to check its value, and use it rather than relying on a single global.

While you are telling us what the robot actually does versus what you want it to do, please explain why echo, ultrasoundValue, and ultraSoundSignal are global variables. If ping() returns a value, the value that it returns should be used, not ignored in favor of a global variable.

Consistency in variable names is a good thing. ultrasoundValue and ultraSoundSignal are not consistent. One has sound with a lower case s, the other has sound with an upper case s. In addition, the ultraSoundSignal contains a pin number, and has nothing to do with any signal.

void loop() 
{
  forward ();
  while (ping () > 5);
  haltMotors();   //stop the motors before reversing direction
  delay(3000);
  backward (); 
  while (ping () < 10) ;  //move robot back 10 inches before turning
  turnRight();
}

I found the code for this sensor on this Arduino site: http://www.arduino.cc/playground/Main/UltrasonicSensor

How does Groove's response look? I'm away from the robot right now, but I'll try it out when I get back.

How does Groove's response look

It looks the same as yours, but with superfluous stuff removed.

I found the code for this sensor on this Arduino site

It's pretty horrible (I would have parameterised the output pin number), and it's never really a good idea to return a global value, things could get confusing.

My take: (caveat: I don't have this sensor, so the code is untested)

const int myPingPin = 9; 

 void setup()
 {
   Serial.begin(9600);
 }

unsigned long ping (const int pingPin)
{
    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);
    pinMode(pingPin, INPUT);
    digitalWrite(pingPin, HIGH); //  I don't think I've seen other Ping code do this - is it necessary?
    unsigned long echo = pulseIn(pingPin, HIGH);
    return (unsigned long) (echo / 58.138) * .39;
}

void loop()
{
    unsigned long x = ping(myPingPin);
    Serial.println(x);
    delay(250);
}

Ok Goove, thanks for your edit. Is my loop still correct? I’m not sure if I am updating the position in the correct spots.

void loop(){
  while (ultrasoundValue < 10)  //move robot back 10 inches before turning
   {
     backward();    void loop()
   {
  while (ping() > 5)  //while robot is 5 inches away from object
    {
      forward();       //go forward
      ping();            // update position
    }
  haltMotors();   //stop the motors before
     ping();                 //update position
    }
  turnRight();
  delay(5000);
}
  while (ultrasoundValue < 10)  //move robot

The first time through loop, you haven’t called ping, so ultrasoundValue is zero, so it is < 10.
Why don’t you try the version I wrote for you?

 backward();    void loop()

Have you actually compiled this?
What happened?

I'll be working on it this afternoon, so I'll let you know how it goes.