#include <Servo.h>
#include <mechButton.h>
#include <blinker.h>

enum        delayStates { sitting, waitTiming, runningServoFwd, runningServoBack };
delayStates ourState;
Servo       aServo;
mechButton  aButton(2);
timeObj     theTimer(5000,false);
timeObj     servoDelay(1000);
blinker     runningBlinker;

void setup() {
  
  aServo.attach(3);
  aServo.write(0);
  aButton.setCallback(buttonClk);
  ourState = sitting;
}


void buttonClk(void) {

  if (ourState==sitting) {
    if (!aButton.trueFalse()) {
      theTimer.start();
      ourState = waitTiming;
      runningBlinker.setOnOff(true);
    }
  }
}


void loop() {
  
  idle();
  switch(ourState) {
    case sitting : break;
    case waitTiming :
      if (theTimer.ding()) {
        aServo.write(180);
        servoDelay.start();
        ourState = runningServoFwd;
      }
    break;
    case runningServoFwd :
      if (servoDelay.ding()) {
        aServo.write(0);
        servoDelay.start();
        ourState = runningServoBack;
      }
    break;
    case runningServoBack : 
      if (servoDelay.ding()) {
        ourState = sitting;
        runningBlinker.setOnOff(false);
      }
    break;
  }
}