I'm programming an arduino uno with attached screen to be a clock and have the code below. It counts up from zero the milliseconds, seconds, minutes and hours and when it hits 24 hours it starts again from zero. Does anyone know how I can set the start time that it automatically starts at when it is reset? It will be a one off thing and I don't want it to interfere with the clock resetting to 0 hours after it hits 24. Thanks
#include <Wire.h>
#include <LiquidCrystal.h>
#define MILLIS_OVERFLOW 34359738
/**
* Clock Variables
*/
unsigned long currentMillis, previousMillis, elapsedMillis;
int seconds, minutes, hours;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
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;
}