#include <Servo.h>
#define ACTIVATED HIGH
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int flag = 0;
int timer = 0;
int switchState = 0;
int lastSwitchState = 0;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(LED_BUILTIN, OUTPUT);
pinMode(9, OUTPUT); ///output to servo
pinMode(5, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
}
void loop() {
digitalRead(3); //switch position: LEFT
digitalRead(5); //switch position: RIGHT
delay(100);
switchState = digitalRead(5);
if (switchState == HIGH && switchState != lastSwitchState){
timer = timer + 1 ; // will increment every loop of the if statement
if(timer == 5){
//myservo.write(0);
//delay(1000);
myservo.write(180);
delay(1000);
}else{
myservo.write(0); // tell servo to go to position in variable 'pos'
delay(1000);
}
}else{
myservo.write(0); // tell servo to go to position in variable 'pos'
delay(1000);
}
lastSwitchState = switchState;
}
//Still runs when only shifted for a second.
// This indicates the program is ignoring the drop out from the while statement.
//now it works it just needs a timer and a dropout condition