My Arduino Mega has been running for 49 days, 17 hours, 2 minutes, 46 seconds

I was worried about milis overflow as I am using Arduino Mega as a main controller in my Home Automation but I have read that I should be fine. Just in case I set up logging around 48 days of uptime and those are the results...

Uptime 4294932 49 days, 17 hours, 2 minutes, 12 seconds
Uptime 4294933 49 days, 17 hours, 2 minutes, 13 seconds
Uptime 4294934 49 days, 17 hours, 2 minutes, 14 seconds
Uptime 4294935 49 days, 17 hours, 2 minutes, 15 seconds
Uptime 4294936 49 days, 17 hours, 2 minutes, 16 seconds
Uptime 4294937 49 days, 17 hours, 2 minutes, 17 seconds
Uptime 4294938 49 days, 17 hours, 2 minutes, 18 seconds
Uptime 4294939 49 days, 17 hours, 2 minutes, 19 seconds
Uptime 4294940 49 days, 17 hours, 2 minutes, 20 seconds
Uptime 4294941 49 days, 17 hours, 2 minutes, 21 seconds
Uptime 4294942 49 days, 17 hours, 2 minutes, 22 seconds
Uptime 4294943 49 days, 17 hours, 2 minutes, 23 seconds
Uptime 4294944 49 days, 17 hours, 2 minutes, 24 seconds
Uptime 4294945 49 days, 17 hours, 2 minutes, 25 seconds
Uptime 4294946 49 days, 17 hours, 2 minutes, 26 seconds
Uptime 4294947 49 days, 17 hours, 2 minutes, 27 seconds
Uptime 4294948 49 days, 17 hours, 2 minutes, 28 seconds
Uptime 4294949 49 days, 17 hours, 2 minutes, 29 seconds
Uptime 4294950 49 days, 17 hours, 2 minutes, 30 seconds
Uptime 4294951 49 days, 17 hours, 2 minutes, 31 seconds
Uptime 4294952 49 days, 17 hours, 2 minutes, 32 seconds
Uptime 4294953 49 days, 17 hours, 2 minutes, 33 seconds
Uptime 4294954 49 days, 17 hours, 2 minutes, 34 seconds
Uptime 4294955 49 days, 17 hours, 2 minutes, 35 seconds
Uptime 4294956 49 days, 17 hours, 2 minutes, 36 seconds
Uptime 4294957 49 days, 17 hours, 2 minutes, 37 seconds
Uptime 4294958 49 days, 17 hours, 2 minutes, 38 seconds
Uptime 4294959 49 days, 17 hours, 2 minutes, 39 seconds
Uptime 4294960 49 days, 17 hours, 2 minutes, 40 seconds
Uptime 4294961 49 days, 17 hours, 2 minutes, 41 seconds
Uptime 4294962 49 days, 17 hours, 2 minutes, 42 seconds
Uptime 4294963 49 days, 17 hours, 2 minutes, 43 seconds
Uptime 4294964 49 days, 17 hours, 2 minutes, 44 seconds
Uptime 4294965 49 days, 17 hours, 2 minutes, 45 seconds
Uptime 4294966 49 days, 17 hours, 2 minutes, 46 seconds
Uptime 0 
Uptime 1 1 second
Uptime 2 2 seconds
Uptime 3 3 seconds
Uptime 4 4 seconds
Uptime 5 5 seconds
Uptime 6 6 seconds
Uptime 7 7 seconds
Uptime 8 8 seconds
Uptime 9 9 seconds
Uptime 10 10 seconds
Uptime 11 11 seconds
Uptime 12 12 seconds
Uptime 13 13 seconds
Uptime 14 14 seconds
Uptime 15 15 seconds
Uptime 16 16 seconds
Uptime 17 17 seconds
Uptime 18 18 seconds
Uptime 19 19 seconds
Uptime 20 20 seconds
Uptime 21 21 seconds
Uptime 22 22 seconds

I hope someone will find this helpful

Also one more time I would like to thank @blh64 for help in topic Delay in reconnect() blocks execution of other code - #8 by blh64

Welcome back @gggg1981gggg
It would be helpful if you were to also post the code used that gave those results.

void uptimeAndPublishRelayStates(){
  static unsigned long timer = 0;
  unsigned long interval = 1000;
  static unsigned long uptime = 0;
  if (millis() - timer > interval){
  timer = millis();
  uptime = timer/1000;
  //Print uptime to serial:
  Serial.print("Uptime: ");
  Serial.println(uptime);
  //Change static unsigned long to CharArray 
  String str = String(uptime); 
  // Length (with one extra character for the null terminator)
  int str_len = str.length() + 1; 
  // Prepare the character array (the buffer) 
  char uptime2mqtt[str_len];
  // Copy it over 
  str.toCharArray(uptime2mqtt, str_len);
  //Publish uptime to mqtt
  client.publish("arduino-mega/uptime",uptime2mqtt);
  publishAllRelaysStates();}
}
1 Like

You do it wrong, count seconds instead and you will never have to worry about uptime being reset

For example this code would count seconds for more than 136 years..

void setup()
{
  Serial.begin( 115200 );
}
 
void loop()
{
  uint32_t t1 = millis();
  static uint32_t t2 = 0;
  static uint32_t seconds = 0;
 
  if ( t1 - t2 >= 1000 )
  {
    t2 = t1;
    Serial.println( ++seconds );
  }
}
1 Like

Arduino still works fine. Just the milis() reset as mentioned in first post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.