I use a lot of Arduino devices in my smart home and often need it to time things over a much longer time period than the millis() function allows. To get around this limitation, my projects and code work with seconds instead of milliseconds and this allows my code to work over >49,000 days without issues. The code I use to achieve this is:
// Global variables
unsigned long millis_new; // time now in millis
unsigned long millis_old; // previous time in millis
unsigned long rollovers = 0;
unsigned long seconds = 0;
void setup()
{
}
// Function to track seconds over long time period
void Timers()
{
millis_new = millis();
if (millis_new < millis_old)
rollovers++;
millis_old = millis_new;
seconds = (0x100000000 * rollovers) + (millis_new / 1000);
}
// Main Program loop
void loop()
{
Timers();
// The rest of my code
Serial.print("Seconds: ");
Serial.println (seconds);
}