You are on page 1of 8

/**************************************************

* Arduino Kitchen Timer v1.0 - 2016/01/27


* By Angelo Fiorillo (Rome, IT)
* This work is distributed under the GNU General
* Public License version 3 or later (GPL3+)
* Please include this credit note if you want to
* re-use any part of this sketch. Respect my work
* as I'll do with yours.
* Feel free to contact me: afiorillo@gmail.com
* ************************************************/
#include <LiquidCrystal.h>
#include <TimeLib.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const
const
const
const
const
const

int
int
int
int
int
int

ledPin = 8;
setupButtonPin = 12;
alarmButtonPin = 13;
plusPin = 10;
minusPin = 11;
selectorPin = 9;

int setupHours =
int setupMinutes
int setupSeconds
time_t setupTime

0;
= 0;
= 0;
= 0;

int alarmHours =
int alarmMinutes
int alarmSeconds
time_t alarmTime

0;
= 0;
= 0;
= 0;

// How many hours will count down when started


// How many minutes will count down when started
// How many seconds will count down when started

int currentAlarmHours = 0;
int currentAlarmMinutes = 0;
int currentAlarmSeconds = 0;
time_t currentAlarmTime = 0;
int currentHours =
int currentMinutes
int currentSeconds
time_t currentTime

0;
= 0;
= 0;
= 0;

time_t startTime = 0;
time_t elapsedTime = 0;
int setupButtonState = LOW;
long setupButtonLongPressCounter = 0;
int alarmButtonState = LOW;
int selectorPinState = LOW;
int upButtonState = LOW;
int downButtonState = LOW;
int setupButtonPrevState = LOW;
int alarmButtonPrevState = LOW;
int upButtonPrevState = LOW;
int selectorPinPrevState = LOW;
int downButtonPrevState = LOW;
bool setupButtonPressed = false;
bool setupButtonLongPressed = false;
bool alarmButtonPressed = false;
bool selectorPress = false;
bool upButtonPressed = false;

bool downButtonPressed = false;


const
const
const
const

int
int
int
int

Time_run = 0;
MODE_SETUP = 1;
ALARM_SETUP = 2;
MODE_BLINKING = 3;

int currentMode = Time_run;

// 0=idle 1=setup 2=running 3=ringing


// Power up --> idle
// Reset --> idle
// Start/Stop --> start or stop counter
// Up / Down --> NOP
// Reset (long press) --> enter setup
// Start/Stop --> data select
// Up --> increase current data value
// Down --> decrease current data value
// Reset --> exit setup (idle)

int dataSelection = 0; // Currently selected data for edit (setup mode, changes
with Start/Stop)
// 0=hours (00-23) 1=minutes (00-59) 2=seconds (00-59)
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2);
pinMode(setupButtonPin, INPUT);
pinMode(alarmButtonPin, INPUT);
pinMode(plusPin, INPUT);
pinMode(minusPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
alarmButtonPressed = false;
upButtonPressed = false;
downButtonPressed = false;
/*
* Reset button management
*/
setupButtonPressed = false;
setupButtonLongPressed = false;
setupButtonState = digitalRead(setupButtonPin);
if(setupButtonState != setupButtonPrevState)
{
setupButtonPressed = setupButtonState == HIGH;
setupButtonPrevState = setupButtonState;
}
else // Long press management...
{
if(setupButtonState == HIGH)
{
setupButtonLongPressCounter++;
if(setupButtonLongPressCounter == 24)
{
setupButtonPressed = false;
setupButtonLongPressed = true;
setupButtonLongPressCounter = 0;

}
}
else
{
setupButtonLongPressCounter = 0;
setupButtonPressed = false;
setupButtonLongPressed = false;
}
}
/*
* Alarm button management
*/
alarmButtonPressed = false;
alarmButtonState = digitalRead(alarmButtonPin);
if(alarmButtonState != alarmButtonPrevState)
{
alarmButtonPressed = alarmButtonState == HIGH;
alarmButtonPrevState = alarmButtonState;
}
selectorPress = false;
selectorPinState = digitalRead(selectorPin);
if(selectorPinState != selectorPinPrevState){
selectorPress = selectorPinState == HIGH;
selectorPinPrevState = selectorPinState;
}
downButtonPressed = false;
downButtonState = digitalRead(minusPin);
if(downButtonState != downButtonPrevState)
{
downButtonPressed = downButtonState == HIGH;
downButtonPrevState = downButtonState;
}
upButtonPressed = false;
upButtonState = digitalRead(plusPin);
if(upButtonState != upButtonPrevState)
{
upButtonPressed = upButtonState == HIGH;
upButtonPrevState = upButtonState;
}
switch(currentMode)
{
case Time_run:
startTime = now();
if(setupButtonPressed)
{
Reset();
}
if(setupButtonLongPressed)
{
currentMode = MODE_SETUP;
}
if(alarmButtonPressed)
{
ALARM_SETUP;

}
break;
case MODE_SETUP:
if(setupButtonPressed)
{
// Exit setup mode
startTime = setupSeconds + (60 * setupMinutes) + (3600 * setupHours);
currentHours = setupHours;
currentMinutes = setupMinutes;
currentSeconds = setupSeconds;
dataSelection = 0;
currentMode = Time_run;
}
if(selectorPress)
{
// Select next data to adjust
dataSelection++;
if(dataSelection == 3)
{
dataSelection = 0;
}
}
if(downButtonPressed)
{
switch(dataSelection)
{
case 0: // hours
setupHours--;
if(setupHours == -1)
{
setupHours = 23;
}
break;
case 1: // minutes
setupMinutes--;
if(setupMinutes == -1)
{
setupMinutes = 59;
}
break;
case 2: // seconds
setupSeconds--;
if(setupSeconds == -1)
{
setupSeconds = 59;
}
break;
}
}
if(upButtonPressed)
{
switch(dataSelection)
{
case 0: // hours
setupHours++;
if(setupHours == 24)
{
setupHours = 0;

}
break;
case 1: // minutes
setupMinutes++;
if(setupMinutes == 60)
{
setupMinutes = 0;
}
break;
case 2: // seconds
setupSeconds++;
if(setupSeconds == 60)
{
setupSeconds = 0;
}
break;
}
}
break;
case ALARM_SETUP:
if(alarmButtonPressed)
{
// Exit setup mode
alarmTime = alarmSeconds + (60 * alarmMinutes) + (3600 * alarmHours);
currentAlarmHours = alarmHours;
currentAlarmMinutes = alarmMinutes;
currentAlarmSeconds = alarmSeconds;
dataSelection = 0;
currentMode = Time_run;
}
if(selectorPress)
{
// Select next data to adjust
dataSelection++;
if(dataSelection == 3)
{
dataSelection = 0;
}
}
if(downButtonPressed)
{
switch(dataSelection)
{
case 0: // hours
alarmHours--;
if(alarmHours == -1)
{
alarmHours = 23;
}
break;
case 1: // minutes
alarmMinutes--;
if(alarmMinutes == -1)
{
alarmMinutes = 59;
}
break;
case 2: // seconds
alarmSeconds--;

if(alarmSeconds == -1)
{
alarmSeconds = 59;
}
break;
}
}
if(upButtonPressed)
{
switch(dataSelection)
{
case 0: // hours
alarmHours++;
if(alarmHours == 24)
{
alarmHours = 0;
}
break;
case 1: // minutes
alarmMinutes++;
if(alarmMinutes == 60)
{
alarmMinutes = 0;
}
break;
case 2: // seconds
alarmSeconds++;
if(alarmSeconds == 60)
{
alarmSeconds = 0;
}
break;
}
}
break;
case MODE_BLINKING:
if(setupButtonPressed || alarmButtonPressed || downButtonPressed || upButt
onPressed)
{
currentMode = Time_run;
digitalWrite(ledPin, LOW);
}
break;
}
/*
* Time management
*/
switch(currentMode)
{
case Time_run:
currentTime = (now() + startTime);
case MODE_SETUP:
// NOP
break;
case ALARM_SETUP:
/*currentTime = (now() + startTime);
if(currentTime >= alarmTime)
{

currentMode = MODE_BLINKING;
}*/
break;
case MODE_BLINKING:
digitalWrite(ledPin, HIGH);
break;
}
/*
* LCD management
*/
//lcd.clear();
lcd.setCursor(0, 0);
switch(currentMode)
{
case Time_run:
lcd.print("Time:");
lcd.setCursor(6, 0);
lcd.print(currentHours);
lcd.print(":");
lcd.print(currentMinutes);
lcd.print(":");
lcd.print(currentSeconds);
lcd.print("
");
lcd.setCursor(0,1);
lcd.print("Alarm:");
lcd.setCursor(7,1);
lcd.print(alarmHours);
lcd.print(":");
lcd.print(alarmMinutes);
lcd.print(":");
lcd.print(alarmSeconds);
lcd.print("
");
break;
case MODE_SETUP:
lcd.print("Setup mode: ");
switch(dataSelection)
{
case 0:
lcd.print("HRS ");
break;
case 1:
lcd.print("MINS");
break;
case 2:
lcd.print("SECS");
break;
}
lcd.setCursor(0, 1);
lcd.print(setupHours);
lcd.print(" ");
lcd.print(setupMinutes);
lcd.print(" ");
lcd.print(setupSeconds);
lcd.print("
");
break;
case ALARM_SETUP:
lcd.print("Setup mode: ");

switch(dataSelection)
{
case 0:
lcd.print("HRS ");
break;
case 1:
lcd.print("MINS");
break;
case 2:
lcd.print("SECS");
break;
}
lcd.setCursor(0, 1);
lcd.print(alarmHours);
lcd.print(" ");
lcd.print(alarmMinutes);
lcd.print(" ");
lcd.print(alarmSeconds);
lcd.print("
");
break;
case MODE_BLINKING:
lcd.print(" ALARM! - ALARM!");
lcd.setCursor(0, 1);
lcd.print("
");
break;
}
delay(10);
}
void Reset()
{
startTime = now();
}