Arduino Uno - Problems with Battery and USB programs

I have written a program and it runs the way I want it to when the arduino is powered through the USB (The USB cord is still plugged in). However, when I unplug the USB, and run the chip through a battery pack, it runs a completely different program (an older version of the currently uploaded one), though a new one has not been uploaded. I have run the clear program several times to no success. Thoughts anyone?

So I played around with it, and its not running an older program, its running only the first few lines of code.
For example, with the code below it will run forward, left, forward on a continious loop instead of going through the rest of it. Once again this is only while it is powered using the battery back. When it is running through USB power, the program runs completely through.

#include <Servo.h>
Servo myservo1;
Servo myservo2;

void setup() {

myservo1.attach(3);
myservo2.attach(8);

forward();
delay(1000);
left();
delay(300);
forward();
delay(1000);
backward();
delay(500);
right();
delay(5000);
forward();
delay(1500);

}

void loop() {
}
void forward() {
myservo1.writeMicroseconds(1300);
myservo2.writeMicroseconds(1700);
}
void backward() {
myservo1.writeMicroseconds(1700);
myservo2.writeMicroseconds(1300);
}
void halt() {
myservo1.writeMicroseconds(1500);
myservo2.writeMicroseconds(1500);
}
void right() {
myservo1.writeMicroseconds(1700);
myservo2.writeMicroseconds(1700);
}
void left() {
myservo1.writeMicroseconds(1300);
myservo2.writeMicroseconds(1300);
}

Are you using a 9V battery?

I see your code runs two servos, that is marginal even on USB power as the current demand for servos really require a amp or two depending on the model of the servo and the mechanical load on the servo. Even if using a strong enough battery (which your not if using a puny 9 volt battery) but using the arduino +5vdc voltage to power the servo is too much of a load for the on board 5 volt regulator. You really should use external +5vdc power to power servos. The arduino was designed to control things not really to also power them.

Lefty

I guess I should have clarified that by battery pack, I am using a 6V external battery pack.

GrahamHogsett: I guess I should have clarified that by battery pack, I am using a 6V external battery pack.

Well a wiring drawing of your complete setup would be helpful. If the sketch runs properly under USB power then the coding should not be in question I guess. Servos often generate noise on the power lines and capacitors properly placed can be helpful.

Lefty