1

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;
}
asked May 7, 2015 at 22:01

1 Answer 1

3

Assume you want the clock to start at 08:42:21, change

int seconds, minutes, hours;

to

int seconds = 21, minutes = 42, hours = 8;

Note that you should always also initialize (as in the second case above), and not only declare (as in the first case) all variables before you first read their values. Otherwise, for local variables you cannot be sure what their initial value is. (Global variables are implicitly initialized to zero if not explicitly initialized.)

answered May 8, 2015 at 6:29
1
  • Your last sentence is incorrect. Global variables, like the ones discussed here, are implicitly initialized to zero if not explicitly initialized. The link you provide to support your assertion is for local variables. Commented Jun 1, 2015 at 10:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.