Hello all,
hopefully i can get some help here. I am trying to have 5 servos react to 5 photoresistor. each resistor controls a single motor. I have it all wired and powering the servos with 10 AA batteries and the arduino is plugged into the wall. When i run the code below, i dont get any feedback. However, when i run each servo and photoresistor seperately, they work. Do i need additional hardware or is there something wrong in my coding? Any advise will be helpful
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
int photopinA = 1;
int photopinB = 2;
int photopinC = 3;
int photopinD = 4;
int photopinE = 5;
int valA = 0;
int valB = 0;
int valC = 0;
int valD = 0;
int valE = 0;
int pos = 0;
void setup ()
{
Serial.begin(9600);
myservo1.attach(6);
myservo2.attach(7);
myservo3.attach(8);
myservo4.attach(5);
myservo5.attach(4);
pinMode(photopinA,INPUT);
pinMode(photopinB,INPUT);
pinMode(photopinC,INPUT);
pinMode(photopinD,INPUT);
pinMode(photopinE,INPUT);
}
void loop()
{
valA = analogRead(photopinA);
valB = analogRead(photopinB);
valC = analogRead(photopinC);
valD = analogRead(photopinD);
valE = analogRead(photopinE);
if(valA <= 500)
{
for(pos = 0; pos >= -180; pos -=90) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo3.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(valA);
} else {
for(pos = 0; pos<= 180; pos +=90) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}
Serial.println(valA);
if(valB <= 500)
{
for(pos = 0; pos >= -180; pos -=90) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo4.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(valB);
} else {
for(pos = 0; pos <= 180; pos +=90) // goes from 180 degrees to 0 degrees
{
myservo4.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}
Serial.println(valB);
if(valC <= 500)
{
for(pos = 0; pos <= 180; pos +=90) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(valC);
} else {
for(pos = 0; pos >= -180; pos -=90) // goes from 180 degrees to 0 degrees
{
myservo2.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}
Serial.println(valC);
if(valD <= 500)
{
for(pos = 0; pos >= -180; pos -=90) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo5.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(valD);
} else {
for(pos = 0; pos <= 180; pos +=90) // goes from 180 degrees to 0 degrees
{
myservo5.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}
Serial.println(valD);
if(valE <= 500)
{
for(pos = 0; pos >= 180; pos -=90) // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
Serial.println(valE);
} else {
for(pos = 0; pos = 180; pos +=90) // goes from 180 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
// waits 15ms for the servo to reach the position
}
}
Serial.println(valE);
}