why does my robot behave differently when the usb is plugged in

I’m doing a collision avoidance (ultra range finder sensor)robot and when I have it plugged in to the usb it reacts just how I want it but when I just have the battery plugged in it acts like it can’t make a decision when it comes to avoiding obstacles. Why does it behave differently do to the power source and how can I fix it? could it have something to do with my code? Does anybody have any suggestions on improving the code I have thus far? my bot waits until it gets to the object to scan then move, it should be doing both on the fly in unison. Like I asked cn somebody give me some pointers on how i can improve this? And is it worth using a xbee modual along with my ultrasonic range finder?

#include <Servo.h> // Enables the Servo library
 
int E1 = 6; //M1 Speed Control 
int E2 = 5; //M2 Speed Control 
int M1 = 8; //M1 Direction Control 
int M2 = 7; //M1 Direction Control

Servo PingServo;
//int IRsensor = 3; //IR interface
int pingPin = 11;//Ping sensor
int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn
int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
long duration, inches, cm; // Define variables for Ping sensor 

void setup() {
PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
int i; 
for(i=5;i<=8;i++) 
//pinMode(M1, OUTPUT);
//pinMode(M2, OUTPUT);

Serial.begin(9600); // Enables Serial monitor for debugging purposes
Serial.println("Serial test!"); // Test the Serial communication

}
void stop(void) //Stop 
{ 
 digitalWrite(E1,LOW); 
 digitalWrite(E2,LOW); 
} 
void advance(char a,char b) //Move forward 
{ 
 analogWrite (E1,a); //PWM Speed Control 
 digitalWrite(M1,HIGH); 
 analogWrite (E2,b); 
 digitalWrite(M2,HIGH);
Serial.println("Going forward"); // Prints a line in the serial moni 
} 
void turn_R (char a,char b) //Turn Right 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,HIGH); 
 analogWrite (E2,b); 
 digitalWrite(M2,LOW); 
delay(1600); // Time required to turn right (1.6 seconds)
Serial.println("Motors going Right"); // Prints a line in the serial monitor

} 
void back_off (char a,char b) //Move backward 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,LOW); 
 analogWrite (E2,b); 
 digitalWrite(M2,LOW);
 delay(1600); // Time Required to go back (1.6 seconds)
 Serial.println("Backward"); // Prints a line in the serial monitor 
} 
void turn_L (char a,char b) //Turn Left 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,LOW); 
 analogWrite (E2,b); 
 digitalWrite(M2,HIGH); 
delay(1600); //Time Required to turn left (1.6)Seconds
Serial.println("Motors going Left");// Prints a line in the serial monitor
} 
void loop() 
{ 
LookAhead();
Serial.print(inches);
Serial.println(" inches"); // Prints a line in the serial monitor
if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/
{
back_off(240, 240);


}else // If not:

{
stop(); // Stop all motors
LookAround(); // Check your surroundings for best route
if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right
{
turn_R(240, 240);
}else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left
{
turn_L(240, 240);
}else if (leftDist&&rightDist<minSafeDist) // If the left and right distance is smaller than the min safe distance (11 inch) go back
{
advance(240, 240); 
delay(110); // Wait 0.11 seconds`cx3qqawA
}
}
}
unsigned long ping() {
pinMode(pingPin, OUTPUT); // Make the Pingpin to output
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches
-------------------------------------*/

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
}

long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) // converts time to a distance
{
return microseconds / 29 / 2;
}

void LookAhead() {
PingServo.write(90);// angle to look forward
delay(175); // wait 0.175 seconds
ping();
}

void LookAround(){
PingServo.write(180); // 180° angle
delay(320); // wait 0.32 seconds
ping();
rightDist = inches; //get the right distance
PingServo.write(0); // look to the other side
delay(620); // wait 0.62 seconds
ping(); 
leftDist = inches; // get the left distance
PingServo.write(90); // 90° angle
delay(275); // wait 0.275 seconds

// Prints a line in the serial monitor
Serial.print("RightDist: ");
Serial.println(rightDist);
Serial.print("LeftDist: ");
Serial.println(leftDist);
Serial.print("CenterDist: ");
Serial.println(centerDist);
}

Can you measure the 5V supply voltage with USB and battery power? Maybe the battery can't provide enough current. What battery are you using?

thanks for the reply
I'm using just a regular 4 triple AAA battery pack that came along with the rover. by the way I'm using the df robot shop rover for this project with a arduino uno and it does seem I'm not getting enough power. How can I get more into the board?
oh yeah I have my servo for the ultrasonic range finder in the 3v and the ultrasonic range finder in the 5v

How is the battery pack connected to the Uno?

its basicly attached to the board by a couple screws. what I’m using is a basic DF Robot Rover (it’s not a normal uno) but as I said it acts like it can barely make a decision on the fly by itself but when the usb is plugged in it does everything quickly. I think I need to design a better sketch.

#include <Servo.h> // Enables the Servo library
 
int E1 = 6; //M1 Speed Control 
int E2 = 5; //M2 Speed Control 
int M1 = 8; //M1 Direction Control 
int M2 = 7; //M1 Direction Control

Servo PingServo;
//int IRsensor = 3; //IR interface
int pingPin = 11;//Ping sensor
int minSafeDist = 11 ; // Minimum distance for ping sensor to know when to turn
int centerDist, leftDist, rightDist, backDist; // Define variables center, left, right and back distance
long duration, inches, cm; // Define variables for Ping sensor 

void setup() {
PingServo.attach(10); // Servo is attached to pin 10 in the motor shield
PingServo.write(90); // Center the Ping sensor (puts it at 90 degrees)
int i; 
for(i=5;i<=8;i++) 
//pinMode(M1, OUTPUT);
//pinMode(M2, OUTPUT);

Serial.begin(9600); // Enables Serial monitor for debugging purposes
Serial.println("Serial test!"); // Test the Serial communication

}
void stop(void) //Stop 
{ 
 digitalWrite(E1,LOW); 
 digitalWrite(E2,LOW); 
} 
void advance(char a,char b) //Move forward 
{ 
 analogWrite (E1,a); //PWM Speed Control 
 digitalWrite(M1,HIGH); 
 analogWrite (E2,b); 
 digitalWrite(M2,HIGH);
Serial.println("Going forward"); // Prints a line in the serial moni 
} 
void turn_R (char a,char b) //Turn Right 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,HIGH); 
 analogWrite (E2,b); 
 digitalWrite(M2,LOW); 
delay(1600); // Time required to turn right (1.6 seconds)
Serial.println("Motors going Right"); // Prints a line in the serial monitor

} 
void back_off (char a,char b) //Move backward 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,LOW); 
 analogWrite (E2,b); 
 digitalWrite(M2,LOW);
 delay(1600); // Time Required to go back (1.6 seconds)
 Serial.println("Backward"); // Prints a line in the serial monitor 
} 
void turn_L (char a,char b) //Turn Left 
{ 
 analogWrite (E1,a); 
 digitalWrite(M1,LOW); 
 analogWrite (E2,b); 
 digitalWrite(M2,HIGH); 
delay(1600); //Time Required to turn left (1.6)Seconds
Serial.println("Motors going Left");// Prints a line in the serial monitor
} 
void loop() 
{ 
LookAhead();
Serial.print(inches);
Serial.println(" inches"); // Prints a line in the serial monitor
if(inches >= minSafeDist) /* If the inches in front of an object is greater than or equal to the minimum safe distance (11 inches), react*/
{
back_off(240, 240);


}else // If not:

{
stop(); // Stop all motors
LookAround(); // Check your surroundings for best route
if(rightDist > leftDist) // If the right distance is greater than the left distance , turn right
{
turn_R(240, 240);
}else if (leftDist > rightDist) // If the left distance is greater than the right distance , turn left
{
turn_L(240, 240);
}else if (leftDist&&rightDist<minSafeDist) // If the left and right distance is smaller than the min safe distance (11 inch) go back
{
advance(240, 240); 
delay(110); // Wait 0.11 seconds`cx3qqawA
}
}
}
unsigned long ping() {
pinMode(pingPin, OUTPUT); // Make the Pingpin to output
digitalWrite(pingPin, LOW); //Send a low pulse
delayMicroseconds(2); // wait for two microseconds
digitalWrite(pingPin, HIGH); // Send a high pulse
delayMicroseconds(5); // wait for 5 micro seconds
digitalWrite(pingPin, LOW); // send a low pulse
pinMode(pingPin,INPUT); // switch the Pingpin to input
duration = pulseIn(pingPin, HIGH); //listen for echo

/*Convert micro seconds to Inches
-------------------------------------*/

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
}

long microsecondsToInches(long microseconds) // converts time to a distance
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) // converts time to a distance
{
return microseconds / 29 / 2;
}

void LookAhead() {
PingServo.write(90);// angle to look forward
delay(175); // wait 0.175 seconds
ping();
}

void LookAround(){
PingServo.write(180); // 180° angle
delay(320); // wait 0.32 seconds
ping();
rightDist = inches; //get the right distance
PingServo.write(0); // look to the other side
delay(620); // wait 0.62 seconds
ping(); 
leftDist = inches; // get the left distance
PingServo.write(90); // 90° angle
delay(275); // wait 0.275 seconds

// Prints a line in the serial monitor
Serial.print("RightDist: ");
Serial.println(rightDist);
Serial.print("LeftDist: ");
Serial.println(leftDist);
Serial.print("CenterDist: ");
Serial.println(centerDist);
}

If it works fine when powered by USB and not by battery I can only conclude that the power supply is the problem. How does the sketch know how the board is powered. Try 4 AA cells and see if there is an improvement.

unfortunately I told you wrong it is 4 AA cells I just sat my bot on the ground by itself and it acted like a scared little pup that could make a decision and as sone as I plugged the USB cored it was dancing all over the place. So I think I need to make some improvements somehow some where I just don't know yet. I'm gonna try like some DC power sources or something.

Are the AA batteries rechargeable NiMH’s or non-rechargeable alkalines/lithium? If it is alkalines, note that the current provided by the batteries goes down as the current capacity is used up. While it might have started at 6v when you started, it might be closer to 4v now. Each rechargeable NiMH battery tends to be 1.2v (4.8v) for most of the life of the battery (they start out at 1.4v, but go down to 1.2, until they are nearly depleted).

Cell phone chargers that put out USB should deliver 5v through the USP port (though you do have to search if you need one that can power more than 500maH). I have this EZOpower pack that I got 2 years ago that powers things well: http://www.amazon.com/EZOPower-Universal-External-Rechargeable-NookColor/dp/B004I0F82I/ref=sr_1_1?ie=UTF8&qid=1385582601&sr=8-1&keywords=ezopower+5000maH

Or you could use something like a step up/step down converter, that will take voltage within limits, and convert it to 5v (you can also get versions that go to 3.3v or have adjustable output). Note, right now pololu has them on sale as part of the US Black Friday/Cyber Monday sales madness): http://www.pololu.com/product/2119

If you do use that, you could use lipo batteries, which tend to be smaller/lighter that AA batteries, but perhaps do not have the capacity.

oh yeah I have my servo for the ultrasonic range finder in the 3v and the ultrasonic range finder in the 5v

Probably not a good idea to power a servo from the arduino itself.