Basically I am writing a simple bit of code to increment every one second and display the time and date, which is frustrating me, and I’m sure I’m overlooking something obvious.
So I will display the time as MJD (Modified Julian Date) HH:MM:SS but also allow for a hardcoded MJD to represent a leap second event, and then correctly display the leap second.
It kind of works, however it skips out 23:59:59.
I eventually plan on porting to plain old AVR so I don’t really want to mess around with arduino libraries.
int second = 0, minute = 0, hour = 0;
long mjd = 55000;
long leap = 55964;
void setup()
{
Serial.begin(9600);
Serial.print("MDJ ");
Serial.print(mjd);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void loop()
{
second++;
if (mjd == leap && hour == 23 && minute == 59 && second == 59) {
second++;
} else {
if (second > 59) {
minute++;
second = 0; // reset seconds to zero
}
}
if (minute > 59) {
hour++;
minute = 0;
}
if (hour > 23) {
mjd++;
hour = 0;
}
Serial.print("MDJ ");
Serial.print(mjd);
Serial.print(" ");
if (hour < 10)
Serial.print("0");
Serial.print(hour);
Serial.print(":");
if (minute < 10)
Serial.print("0");
Serial.print(minute);
Serial.print(":");
if (second < 10)
Serial.print("0");
Serial.println(second);
delay(1000);
}
I think you might need only a very small change:
The problem appears to be that you increment to the next second, and then if it’s 23:59:59 and a leap second is indicated, you increment again to 60 (skipping 59).
What you probably want to do is roll over the second unless it is a leap second.