#include <Servo.h>
// constant variables used to set servo angles, in degrees
const int straight = 90;
const int divergent = 110;
// constant variables holding the ids of the pins we are using
const int buttonpinA = 8;
const int servopinA = 9;
const int buttonpinB = 6;
const int servopinB = 7;
// servo movement step delay, in milliseconds
const int step_delay = 70;
// create a servo object
Servo myservoA;
Servo myservoB;
// global variables to store servo position
byte button_stateA;
byte posA = straight; // current
byte old_posA = posA; // previous
byte button_stateB;
byte posB = straight; // current;
byte old_posB = posB;
void setup() {
// set the mode for the digital pins in use
pinMode(buttonpinA, INPUT);
pinMode(buttonpinB, INPUT);
// setup the servo
myservoA.attach(servopinA); // attach to the servo on pin 9
myservoA.write(posA); // set the initial servo position
myservoB.attach(servopinB); // attach to the servo on pin 7
myservoB.write(posB); // set the initial servo position
}
void loop() {
button_stateA = digitalRead(buttonpinA);
if(button_stateA == HIGH){
old_posA = posA;
posA = posA == straight ? divergent: straight;
if(old_posA < posA){
for(int i = old_posA + 1; i <= posA; i++){
myservoA.write(i);
delay(step_delay);
}
}
else {
for(int i = old_posA - 1; i >= posA; i--){
myservoA.write(i);
delay(step_delay);
}
}
}
button_stateB = digitalRead(buttonpinB);
if(button_stateB == HIGH){
old_posB = posB;
posB = posB == straight ? divergent: straight;
if(old_posB < posB) {
for(int i = old_posB + 1; i <= posB; i++){
myservoB.write(i);
delay(step_delay);
}
}
else {
for(int i = old_posB - 1; i >= posB; i--){
myservoB.write(i);
delay(step_delay);
}
}
}
}// end of loop