Obstacle Avoiding Robot Troubleshoot

So I am constructing a simple wall avoiding robot. It has four continous rotation servos for the wheels, one standard servo for it's "neck", and a ping sensor for the head. I've been troubleshooting it for a few days now, and can't figure out if my issue lies in the code, or the actual connections. Right now, all 5 servos and ping sensor are running off the arduino's 5v output. I have a 5v regulator in the mail, that I might be able to split the servos power to their own supply, but I don't know if this is the issue. The device likes to take the same turn everytime it starts, and I'm not sure if I am getting and comparing the ping sensor values correctly. Here is the current code the device is running.

#include <Servo.h>

const int pingPin = 7;
const int LForward = 180;
const int LBackward = 0;
const int RForward = 0;
const int RBackward = 180;

Servo leftFront;
Servo leftBack;
Servo rightFront;
Servo rightBack;
Servo headServo;


void setup(){
  leftFront.attach(5);
  rightFront.attach(9);
  leftBack.attach(6);
  rightBack.attach(10);
  headServo.attach(3);
  headServo.write(90);
}

void loop()
{
   long compareValue;
   long leftValue;
   long rightValue;
   
   compareValue = ping();
   
   if (compareValue < 1500){
      stopMotor();
      headServo.write(0);
      delay(1000);
      leftValue = ping();
      headServo.write(180);
      delay(1000);
      rightValue = ping();
      
      if ( leftValue < rightValue){
        headServo.write(90);
        goLeft();
        goForward();
      } else{
        headServo.write(90);
        goRight();
        goForward();
      }
   
   } else {
     goForward();
   }
}

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

void goLeft(){
  leftFront.write(LBackward);
  leftBack.write(LBackward);
  rightFront.write(RForward);
  rightBack.write(RForward);
  delay(1000);
}
void goRight(){
  leftFront.write(LForward);
  leftBack.write(LForward);
  rightFront.write(RBackward);
  rightBack.write(RBackward);
  delay(1000);
}
void goForward(){
  leftFront.write(LForward);
  leftBack.write(LForward);
  rightFront.write(RForward);
  rightBack.write(RForward);
  delay(500);
      }
void goBackward(){
  leftFront.write(LBackward);
  leftBack.write(LBackward);
  rightFront.write(RBackward);
  rightBack.write(RBackward);
  delay(500);
      }
void stopMotor(){
  leftFront.write(90);
  leftBack.write(90);
  rightFront.write(90);
  rightBack.write(90);
  delay(1000);
      }

Basic function is roll forward, if obstacle is detected, stop, look left, take a reading, look right, take a reading, compare the reading, and then head in the direction that grants the most clearance, at least that's the goal.

Thanks!

all 5 servos and ping sensor are running off the arduino's 5v output.

Well that might not be the cause of your problem at the moment, but you're bound to cause a long-term problems with the Arduino. Forum wisdom is to budget 1A per servo motor, so if your drive wheels are all drawing max current as it starts to move, there's 4A. Arduino can supply 100s of mA, I forget the exact figure.... 4-500?

Basic function is roll forward, if obstacle is detected, stop, look left, take a reading, look right, take a reading, compare the reading, and then head in the direction that grants the most clearance, at least that's the goal.

So, now we know what the code is supposed to do. Now, what does it actually do? In other words, what is the problem? Besides incorrectly powering the servos.

Thanks for the replies. I was fairly certain the draw by the servos was one problem, and it will probably need to be resolved before I can proceed further, thankfully, my 5v regulator, and AA/9v battery holders arrive today. The robot defiantly seems to function better when tethered via USB then running off a 9 volt, so I'm sure by providing the motors their own power, it will address one issue. I'll post the results once the parts arrive, and let you know if it still continues it's issues. Thanks for the responses!

Alright parts are in! The issue was for sure from trying to power the five servo motors off of the Arduino. I skipped the 5v regulator, and went straight to using 4 AA's, as I heard they were better than 9v for current, and it appeared to be the simpler conclusion. Once I gave the motors their own power, and attached the ground properly, it appeared to be working as hoped, except it would turn towards the closest obstacle instead of away. I edited the code above to incorporate the simple fix, for anyone who comes after.

Thanks for the help! Glad it was such a simple issue, but thanks for confirming it for me!

Glad to hear you got it working and thanks for posting your code. If you are planning to mess with arduino and robotics more, I suggest you get a few extra rechargeable AAs and use a 6 AA pack of rechargeables along with a 5v voltage regulator (or a 2S li-po battery with a dedicated charger) to get consistent 5v power.