Go Down

Topic: 5 servo motors with 5 photoresistors (Read 811 times) previous topic - next topic

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

Fletcher Chr

Hi Hedden9414

Code: [Select]
int photopinA = 1;
int photopinB = 2;
int photopinC = 3;
int photopinD = 4;
int photopinE = 5;


You nomenclature for the analog pins are wrong:
Quote
Pin mapping
The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc.


-Fletcher

MarkT

Negative angles for servo?? that can't work, the value is converted to pulse widths, expect 0 to 180 only to work.

You seem to want to smoothly move each servo in a loop (delay statements missing?) - but to run 5 in parallel you can't code like this, you need to maintain state for each channel and drive them in event-driven manner - you dont have the luxury of hogging the CPU for one servo at a time.

Also you should note the code button (#) for quoting code.
[ I won't respond to messages, use the forum please ]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy