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;
}