I'm making a clock using an arduino uno and a compatible freetronics screen. So far it can count the time in a 24 hour loop after which it repeats and counts again. The screen had a number of buttons built onto it and I want to get it so that when one of the buttons on the screen is pressed it prints the time that it was pressed on the top line of the screen while continuing to count and print the time on the second line of the screen.
How can I edit my code below to do this when a button is pressed? What sort of command do I need to add?
#include <Wire.h>
#include <LiquidCrystal.h>
#define MILLIS_OVERFLOW 34359738
/**
* Clock Variables
*/
unsigned long currentMillis, previousMillis, elapsedMillis;
int seconds = 0, minutes = 0, hours = 8;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
Serial.begin(9600);
lcd.begin( 16, 2 );
}
void loop()
{
setClock();
/**
* After set clock now you have 3 int variables with the current time
*/
//seconds
//minutes
//hours
lcd.setCursor ( 0, 1);
lcd.print(hours);
lcd.print(":");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
lcd.print(":");
lcd.print(elapsedMillis);
}
void setClock()
{
currentMillis = millis();
/**
* The only moment when currentMillis will be smaller than previousMillis
* will be when millis() oveflows
*/
if (currentMillis < previousMillis){
elapsedMillis += MILLIS_OVERFLOW - previousMillis + currentMillis;
} else {
elapsedMillis += currentMillis - previousMillis;
}
/**
* If we use equals 1000 its possible that because of the mentioned loop limitation
* you check the difference when its value is (999) and on the next loop its value is (1001)
*/
if (elapsedMillis > 999){
seconds++;
elapsedMillis = elapsedMillis - 1000;
}
if (seconds == 60){
minutes++;
seconds = 0;
}
if (minutes == 60){
hours++;
minutes = 0;
}
if (hours == 24){
hours = 0;
}
previousMillis = currentMillis;
}
Any help will be greatly appreciated, thanks