Hey All,
Working on my first serious arduino project and I need a bunch of servos to turn for a set amount of time and then stop. I managed to write a little piece of code that works for a single servo (see below) but when I add a second servo to the code some funny things happen:
The single servo code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int count = 0;
boolean hiend = false;
boolean lowend = false;
void setup()
{
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
if ((val > 625) && (hiend == false)) { // sets a point slightly above the halfway point to provide a bit of give before servo starts
count++; // starts a counter going to time how long the servo is turning
lowend = false;
myservo.write(180); // sets servo rotation speed (not angle for continuous servo)
delay(200);
if (count > 25) { // sets the upper limit for the servo counter
// this makes the loop stay at 25 (the stop servo point) until the direction is changed
hiend = true;
myservo.write(90); // once counter hits limit this sets the servo to a stop angle (should be close to 90)
Serial.print("hi");
}
} else
if ((val < 425) && (lowend == false)) { // sets a point slightly below the halfway point before servo starts other direction
count--; // starts the count going down
hiend = false;
myservo.write(5);
delay(200);
if (count < -25) { // same as above but for lower limit - basically the counter will move from 25 to -25
lowend = true;
myservo.write(90); // So you have a set limit on how long the servo will turn in each direction before stopping
Serial.print("hi");
}
} else
myservo.write(90);
Serial.println(count); // just to test the count/loop is working
}
The two (hopefully 6 at the end) servo code:
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
int potpin1 = 0; // analog pin used to connect the potentiometer
int val1; // variable to read the value from the analog pin
int count1 = 0;
boolean hiend1 = false;
boolean lowend1 = false;
int ledPin = 13; // LED connected to digital pin 13
Servo myservo3; // create servo object to control a servo
int potpin3 = 5; // analog pin used to connect the potentiometer
int val3; // variable to read the value from the analog pin
int count3 = 0;
boolean otherhi = false;
boolean otherlow = false;
void setup()
{
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo3.attach(3); // attaches the servo on pin 11 to the servo object
}
void loop()
{
val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
if ((val1 > 800) && (hiend1 == false)) { // sets a point slightly above the halfway point to provide a bit of give before servo starts
count1++; // starts a counter going to time how long the servo is turning
lowend1 = false;
myservo1.write(180); // sets servo rotation speed (not angle for continuous servo)
delay(200);
if (count1 > 25) { // sets the upper limit for the servo counter
// this makes the loop stay at 25 (the stop servo point) until the direction is changed
hiend1 = true;
myservo1.write(90); // once counter hits limit this sets the servo to a stop angle (should be close to 90)
}
} else
if ((val1 < 300) && (lowend1 == false)) { // sets a point slightly below the halfway point before servo starts other direction
count1--; // starts the count going down
hiend1 = false;
myservo1.write(5);
delay(200);
if (count1 < -25) { // same as above but for lower limit - basically the counter will move from 25 to -25
lowend1 = true;
myservo1.write(90); // So you have a set limit on how long the servo will turn in each direction before stopping
}
} else
myservo1.write(90);
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
if ((val3 > 800) && (otherhi == false)) { // sets a point slightly above the halfway point to provide a bit of give before servo starts
count3++; // starts a counter going to time how long the servo is turning
otherlow = false;
myservo3.write(180); // sets servo rotation speed (not angle for continuous servo)
delay(200);
if (count3 > 25) { // sets the upper limit for the servo counter
otherhi = true; // this makes the loop stay at 25 (the stop servo point) until the direction is changed
myservo3.write(90); // once counter hits limit this sets the servo to a stop angle (should be close to 90)
}
} else
if ((val3 < 300) && (otherlow == false)) { // sets a point slightly below the halfway point before servo starts other direction
count3--; // starts the count going down
otherlow = false;
myservo3.write(5);
delay(200);
if (count3 < -25) { // same as above but for lower limit - basically the counter will move from 25 to -25
otherlow = true;
myservo3.write(90); // So you have a set limit on how long the servo will turn in each direction before stopping
}
} else
myservo3.write(90);
}
But when I run the second code its works until one of the servos (seems like always the second one) get to the end... it pauses for a second then it keeps moving in the same direction - then oddly enough it starts controlling both servos !?!?! and neither are limited... any help would be greatly appreciated.