Millis Rollover Workaround

Is there a way to directly access timer0_overflow_count? I need to be able to use the millis() function in an application that will be running much longer than 9 hours. If I can detect an impending rollover, I could execute a tight code loop to get me past the rollover point.

You can access it by putting:

extern unsigned long timer0_overflow_count;

at the top of your sketch. However, there's probably a better way to deal with the overflow. For example, if you just need to be able to get the duration between multiple calls to millis(), you should be able to do a millis() - previous_millis (which should work past the rollover).

Or, you could do something like:

current_millis_value = millis(); m += current_millis_value - previous_millis_value; // should work even when millis rolls over seconds += m / 1000; m = m % 1000; minutes += seconds / 60; seconds = seconds % 60; hours += minutes / 60; minutes = minutes % 60; previous_millis_value = current_millis_value;

to know how long it's been since your sketch started.

What are you trying to do?

I’m using the millis value as part of a routine to flash digits on the LED display as the user is setting the clock via a keypad. I’m using the SPI interface to a DS1305 to do the actual timekeeping and alarm functions. I want to be able to do other tasks while in the time setting loop, so I’m not using delay, but just periodically checking millis. Because I’m talking to the seven segment displays directly (so I can define my own characters, I’m going to connect 3 Arduinos together, via SPI, 2 to control the deisplay and one to work with the keypad and RTC. I’ll also use the analog inputs eventually so I can display indoor and outdoor temperature as well as daily high and low temperature.