Arduino won't properly run sketch without USB plugged in

I am using an uno with one motor shield. I am using 2 12v 3000mAh batteries to power the shield and board. My code runs perfectly when the usb is connected to the board and doesn't run right at all without it. Any suggestions? Below is my code.

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
const int numReadings = 10;
const int numChannels = 4;
const int INPUTS = 4;
const byte inputPin[INPUTS] = { A2, A3, A4, A5 };

int readings[numChannels][numReadings]; // the readings from the analog input

int index; // the index of the current reading

int total; // the running total
int total1;
int total2; // the running total
int total3;
int total4; // the running total

int average;
int average1;
int average2;
int average3;
int average4;

// the running total

const int analogOutPin = 9;
const int analogOutPin1 = 10;
const int analogOutPin2 = 11;
const int analogOutPin3 = 12;
const int analogOutPin4 = 13;

int sensorValue = 0; // value read from the pot
int outputValue, oldout;

int sensorValue1 = 0;
int outputValue1, oldout1;

int sensorValue2 = 0; // value read from the pot
int outputValue2, oldout2;

int sensorValue3 = 0;
int outputValue3 = 0;

int sensorValue4 = 0;
int outputValue4 = 0;

void setup(){

// initialize serial communication with computer:
Serial.begin(9600);

myservo1.attach(9);
myservo2.attach(10);
myservo3.attach(11);

}

void loop() {
total = total - readings[0][index];
// read from the sensor:
readings[0][index] = analogRead(inputPin[0]); //Index Finger
// add the reading to the total:
total = total + readings[0][index];
// advance to the next position in the array:
index = index + 1;

if (outputValue < (oldout-2) || outputValue > (oldout+2)){ //dead band
myservo1.write(outputValue); //position the servo
}

oldout=outputValue;

total1 = total1 - readings[1][index];
// read from the sensor:
readings[1][index] = analogRead(inputPin[1]); //Middle Finger
// add the reading to the total:
total1 = total1 + readings[1][index];
// advance to the next position in the array:
index = index + 1;

if (outputValue1 < (oldout1-2) || outputValue1 > (oldout1+2)){ //dead band
myservo2.write(outputValue1); //position the servo
}

oldout1=outputValue1;

total2 = total2 - readings[2][index];
// read from the sensor:
readings[2][index] = analogRead(inputPin[2]); //Ring/Pinky Finger
// add the reading to the total:
total2 = total2 + readings[2][index];
// advance to the next position in the array:
index = index + 1;

if (outputValue2 < (oldout2-2) || outputValue2 > (oldout2+2)){ //dead band
myservo3.write(outputValue2); //position the servo
}
oldout2=outputValue2;

// if we're at the end of the array...
if (index >= numReadings) {
// ...wrap around to the beginning:
index = 0;
}

average = total/numReadings;
outputValue = map(average, 218, 260, 0, 180); // First Average

average1 = total1/numReadings;
outputValue1 = map(average1, 0, 176, 0, 180); // Second Average

average2 = total2/numReadings;
outputValue2 = map(average2, 0, 172, 0, 180); // Third Average

Serial.print("Index = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
Serial.println(average);

Serial.print("Middle = ");
Serial.print(sensorValue1);
Serial.print("\t output1 = ");
Serial.println(outputValue1);
Serial.println(average1);

Serial.print("Ring = ");
Serial.print(sensorValue2);
Serial.print("\t output2 = ");
Serial.println(outputValue2);
Serial.println(average2);
delay;

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

Please provide details of how you are powering the Arduino board and motors with your batteries.