#include <Arduino.h> //Just in case
#include <Servo.h> //Standard AVR 8bit Servo library
#define LED LED_BUILTIN //Pin 13 is the Inbuilt LED
#define PERIOD 3000UL //5 second time period
#define HOME 40 //Servo HOME position
#define POS1 140 //Servo second position
bool run = 0; //Run sequence flag
uint32_t previousMillis = 0; //Capture current clock to start the timer
Servo servo; //Create a Servo object
void falling (void){ //Interrupt statement
detachInterrupt(0); //Detach interrupt while a timer is running
run = true; //Run flag is true now
servo.write(POS1); //Move servo
digitalWrite(LED, HIGH); //Turn on the LED for a ON visual
previousMillis = millis(); //Starts out timer
}
void setup() {
pinMode(LED, OUTPUT); //Creates the LED Pin as Output
pinMode(2, INPUT_PULLUP); //Use the internal "PULLUP" for Active LOW (falling)
servo.attach(14); //Attach servo to A0 (D14)
servo.write(HOME); //Move servo to start position
attachInterrupt(0, falling, FALLING); //Attach interrupt and wait for it to trigger
}
void loop() {
if(run){ //Only when TRUE we look inside
if(millis() - previousMillis >= PERIOD){ //5 second timer when run is true
servo.write(HOME); //Move servo home after 5 seconds
run = false; //Clear run flag
digitalWrite(LED, LOW); //Turn off LED
attachInterrupt(0, falling, FALLING); //re-attach interrupt for new trigger watch
}
}
}