Hi guys
if the position of the servos is between (0-50) degrees , when button is pressed
servo (a) moves from (0-50) to 180 degrees , then servo (b) motor moves from(0-50) to 180 degrees
however, when button is pressed if the position of the servo is between (180-90) degrees, then servo (b) moves from (180-90) to 0 degrees, then servo (a) moves from (180-90) to 0 degrees
I added led, to show when motors are moving.
#include <Servo.h>
Servo myservoa; // create first servo object to control First servo named myservoa
Servo myservob; // create second servo object to control Second servo named myservob
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int pos2; // place holder for pos value
int inPin = 7; // choose the input pin(for a pushbutton)
int ledPin = 12; // choose the pin for the LED
int val = 0;
void setup() {
myservoa.attach(9); // attaches the servo(a) on pin 9 to the servo object
// myservob.attach(3); // attaches the servo(b) on pin 10 to the servo object
pinMode(ledPin,OUTPUT); // declare LED as output
pinMode(inPin,INPUT); // declare pushbutton as input
}
void loop() {
val = digitalRead(inPin); // set the val
if (val == LOW){
digitalWrite(ledPin, LOW); // turn LED OFF
}
else {
digitalWrite(ledPin,HIGH); // turn LED ON
if (pos == 0) // goes from 0 degrees to 180 degrees
// when the
{
pos2 = pos;
for (pos2 == 0 ; pos2 <= 180; pos2 +=10){
myservoa.write(pos2); // tell servo(a) to go to position variable 'pos'
delay(25); //waits 1.5 seconds for the for servo(a) to reach position
}
pos2 = pos;
for (pos2 == 0; pos2 <= 180; pos2 +=10){
myservob.write(pos2); // tell servo(a) to go to position variable 'pos'
delay(25); //waits 1.5 seconds for the for servo(a) to reach position
pos2+=10;
}
}
else { // goes from 180 degrees to 0 degrees
pos2 = pos;
for (pos2 >0; pos2 >= 0; pos2 -=10){
myservoa.write(pos2); // tell servo(a) to go to position variable 'pos'
delay(25); //waits 1.5 seconds for the for servo(a) to reach position
}
pos2 = pos;
for (pos2 >0; pos2 >=0; pos -=10){
myservoa.write(pos2); // tell servo(a) to go to position variable 'pos'
delay(25); //waits 1.5 seconds for the for servo(a) to reach position
pos2-=10;
}
pos = 0;
}
}
}