//Coundtown Timer for garage project
#include "Timer.h"
#define startStop 7
#define reset 8
Timer timer;
int timeDelay = 1000;
bool timerState = 0;
bool started = 0;
byte hoursStart = 1;
byte minutesStart = 0;
byte secondsStart = 5;
byte hours = 1;
byte minutes = 0;
byte seconds = 5;
unsigned long lastPress = millis();
unsigned long lastTick = millis();
unsigned long time;
void setup() {
Serial.begin(9600);
pinMode(startStop, INPUT);
pinMode(reset, INPUT);
timer = Timer(20000);
}
void loop() {
if (digitalRead(startStop) && millis() - lastPress >= 500) {
timerState = !timerState;
if (timerState && !started) {
timer.start();
started = true;
} else if (!timerState && started) {
timer.pause();
} else if (timerState && started) {
timer.resume();
}
//Serial.println(timerState);
lastPress = millis();
} else if (digitalRead(reset) && millis() - lastPress >= 200) {
timerState = false;
started = false;
}
if (timerState && millis() - lastTick >= timeDelay) {
timerState = timer.getState();
lastTick = millis();
time = timer.getTime();
Serial.println(time);
if (!timerState)
started = false;
}
}
bool timerRun(byte resetState) {
switch (resetState) {
case 0:
if (seconds > 0 || minutes > 0 || hours > 0) {
if (seconds == 0 && minutes > 0) {
minutes --;
seconds = 59;
printTimer(hours, minutes, seconds);
return true;
} else if (seconds == 0 && hours > 0 ) {
hours--;
minutes = 59;
seconds = 59;
printTimer(hours, minutes, seconds);
return true;
} else if (seconds != 0) {
seconds --;
printTimer(hours, minutes, seconds);
return true;
}
} else if (seconds == 0) {
Serial.println("Timer Complete");
return false;
}
break;
case 1:
hours = hoursStart;
minutes = minutesStart;
seconds = secondsStart;
Serial.println("Timer Reset");
delay(500);
return false;
}
}
void printTimer(byte hours, byte minutes, byte seconds) {
Serial.print(hours);
Serial.print(":");
if (minutes < 10) {
Serial.print("0");
}
Serial.print(minutes);
Serial.print(":");
if (seconds < 10) {
Serial.print("0");
}
Serial.println(seconds);
}