// You will need to install LC_baseTools from the IDE library
// mamanger to get this to compile.

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

mechButton  coinClicker(2);
mechButton  servoBtn(12);
timeObj     timeOutTimer(55,false);
timeObj     servoTimer(2000,false);
Servo       theServo;
bool        servoOpen;
float       centsPerEuro = 100;
float       cash;
float       total;

void setup() {
  
  Serial.begin(115200);
  theServo.attach(11);
  theServo.write(0);
  servoOpen = false;
  cash = 0;
  total = 0;
  coinClicker.setCallback(gotCoinClk);
  servoBtn.setCallback(gotServoClk);
}

void gotCoinClk(void) {

  if (!coinClicker.trueFalse()) {
    cash = cash + .05;
    timeOutTimer.start();
  }
  if (cash==1) {
    cash = centsPerEuro;
  }
}


void gotServoClk(void) {

  if (!servoBtn.trueFalse()&&(cash>1||total>1||cash+total>1)) {
    if (!servoOpen) {
      cash = 0;
      total = 0;
      theServo.write(180);
      servoOpen = true;
      Serial.print("Servo clicked! New total: ");
      Serial.print(total/(centsPerEuro/100.0));
      Serial.println(" euros.");
      servoTimer.start();
    }
  }
}


void loop() {

  idle();
  if (timeOutTimer.ding()) {
    total = total + cash;
    cash = 0;
    Serial.print("Coin inserted! New total: ");
    Serial.print(total/(centsPerEuro/100.0));
    Serial.println(" euros.");
    timeOutTimer.reset();
  }
  if (servoOpen && servoTimer.ding()) {
    theServo.write(0);
    servoTimer.reset();
    servoOpen = false;
  }
}