Code check

So i building a litle robot. it is using 2 6v motors and a adafruit shield…

#include <ServoTimer1.h>
#include <AFMotor.h>

int minSafeDist = 7; //Minimum distance

ServoTimer1 servo1;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int pingPin = 2;
unsigned long pinPingvalue = 0;
int leftDist = 0;
int rightDist = 0;
int centerDist = 0;

long duration, inches, cm;


void setup() {
   servo1.attach(10);
   servo1.write(80);
   motor1.setSpeed(200); // set the speed to 200/255
   motor2.setSpeed(200); // set the speed to 200/255
   Serial.begin(9600);           // set up Serial library at 9600 bps
   Serial.println("Serial test!");
}

unsigned long ping() {
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
   pinMode(pingPin,INPUT);
   duration = pulseIn(pingPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
}

long microsecondsToInches(long microseconds)
  {
   return microseconds / 74 / 2;
  }
  long microsecondsToCentimeters(long microseconds)
  {
   return microseconds / 29 / 2;
  }

void AllStop() {
   motor1.run(RELEASE); // turn it off
   motor2.run(RELEASE); // turn it off
}

void AllForward() {
   motor1.run(FORWARD); // turn it on going forward
   motor2.run(FORWARD); // turn it on going forward
   Serial.println("Motors going Forward");
}

void AllBackward() {
   motor1.run(BACKWARD); // turn it on going backward
   motor2.run(BACKWARD); // turn it on going backward
   Serial.println("Motors going Backward");
}

void MoveRight() {
   motor1.run(RELEASE); // turn it off
   motor2.run(FORWARD); // turn it on going forward
   Serial.println("Motors going Right");
}

void MoveLeft() {
   motor1.run(FORWARD); // turn it on going forward
   motor2.run(RELEASE); // turn it off
   Serial.println("Motors going Left");
}

void LookAround() {
   servo1.write(160);
   delay(175);
   rightDist = ping();
   servo1.write(80);
   delay(175);
   centerDist = ping();
   servo1.write(0);
   delay(175);
   leftDist = ping();
   servo1.write(80);
   delay(175);
   Serial.print("RightDist: ");
   Serial.println(rightDist);
   Serial.print("CenterDist: ");
   Serial.println(centerDist);
   Serial.print("LeftDist: ");
   Serial.println(leftDist);
}

void LookAhead() {
   servo1.write(80);
   delay(175);
   centerDist = ping();
   Serial.print("Center distance is: ");
   Serial.println(inches);
}

void loop() {

   LookAhead();
   //LookAround();

   if(centerDist >= minSafeDist){
     AllForward();

     delay(100);
    }else{
     //stop
     AllStop();
     delay(100);

     //have a look around
     LookAround();
     if(rightDist == leftDist)){
       //best to turn around
       MoveLeft();  
       delay(2000);
     //go in the direction that read farthest
     }else if(leftDist > rightDist){
       //best to turn left
       MoveLeft();
       delay(500);
     }else if(rightDist >leftDist){
       //best to turn right
       MoveRight();
       delay(500);
}
    }
}

The problem with this code is that when it gets close to a wall >7 it will try to turn either left or right but then when it turns if there is a wall close by it will record distance 6-5-4-3-2-1-1-1-1-1 and will just hit the wall with out turning when it gives the 10th dist is when it tries to turn… Why is it waiting for 10 records??

The problem with this code is that when it gets close to a wall >7
it will try to turn either left or right but then when it turns if there is a
wall close by it will record distance 6-5-4-3-2-1-1-1-1-1 and will just hit
the wall with out turning when it gives the 10th dist is when it tries to
turn.. Why is it waiting for 10 records??

It would be helpful to see the whole output. Do those numbers represent left distance, center distance, or right distances only, or some combination of distances?

Output is similiar to this:

Center distance is: 175
Motors going Forward
Center distance is: 173
Motors going Forward

*Put something in front of it*

Center distance is: 6
Motors going Forward

RightDist: 24
CenterDist: 4
LeftDist: 20
Motors going Right

*Here is the problem*
Center distance is: 20
Motors going Forward
Center distance is: 15
Motors going Forward
Center distance is: 10
Motors going Forward
Center distance is: 5
Motors going Forward
Center distance is: 3
Motors going Forward
Center distance is: 1
Motors going Forward
Center distance is: 1
Motors going Forward
Center distance is: 1
Motors going Forward
Center distance is: 1
Motors going Forward
Center distance is: 1
Motors going Forward

RightDist: 175
CenterDist: 1
LeftDist: 120
Motors going Right

*But at this point the robot is stuck and cant move...*

The only thing that pops out at me is this:

unsigned long ping() {
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
   pinMode(pingPin,INPUT);
   duration = pulseIn(pingPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
}

The function is defined as returning an unsigned long, but there is no return statement, returning anything.

Then, it's output is stored in an int:

int leftDist = 0;
int rightDist = 0;
int centerDist = 0;

   rightDist = ping();

Try adding a return statement to ping(), and correcting the return type. Both microsecondsToInches and miscrosecondsToCentimeters return long, to ping probably should, too.

Then, make the variables that hold the output from ping are the same size as the output from ping.