Close

Updated Uptime Counter

A project log for Hacking the way to growing food

Using Technology And A Hackers Mindset To Grow Food. Last Updated [16/01/2021]

michael-ratcliffeMichael Ratcliffe 09/12/2015 at 16:286 Comments

This is an updated version of the uptime counter here:

https://hackaday.io/project/7008-fly-wars-a-hackers-solution-to-world-hunger/log/24201-just-a-few-useful-arduino-hacks-sram-uptime/discussion-35562

This one also takes into account the rollover of the timers at 50 days, so it will give a estimate of uptime for a very long time [years].

/* This Script is the bare bones needed to Keep a Uptime counter that will survive the 50 day timer rollover
This will not give a uptime of great accuracy over long periods, but it will let you see if your arduino has reset
if you want better accuracy, pull the Unix time from the IOT, External RTC or GPS module
Also Reconnecting the serial com's will reset the arduino. So this is mainly useful for a LCD screen

Michael Ratcliffe  Mike@MichaelRatcliffe.com
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .

    
*/

//************************** Just Some basic Definitions used for the Up Time LOgger ************//
long Day=0;
int Hour =0;
int Minute=0;
int Second=0;
int HighMillis=0;
int Rollover=0;


//************** Setup routine - Runs once at power up **************************//
void setup(){
Serial.begin(9600); // starting Serial Com's
  
};

//****** Main Loop - Put your Code Here ********************//
void loop(){
 
uptime(); //Runs the uptime script located below the main loop and reenters the main loop
print_Uptime();
delay(500);
};



//************************ Uptime Code - Makes a count of the total up time since last start ****************//

 void uptime(){
//** Making Note of an expected rollover *****//   
if(millis()>=3000000000){ 
HighMillis=1;

}
//** Making note of actual rollover **//
if(millis()<=100000&&HighMillis==1){
Rollover++;
HighMillis=0;
}

long secsUp = millis()/1000;

Second = secsUp%60;

Minute = (secsUp/60)%60;

Hour = (secsUp/(60*60))%24;

Day = (Rollover*50)+(secsUp/(60*60*24));  //First portion takes care of a rollover [around 50 days]
                       
};

//******************* Prints the uptime to serial window **********************//
void print_Uptime(){
  
  Serial.print(F("Uptime: ")); // The "F" Portion saves your SRam Space
  Serial.print(Day);
  Serial.print(F("  Days  "));
  Serial.print(Hour);
  Serial.print(F("  Hours  "));
  Serial.print(Minute);
  Serial.print(F("  Minutes  "));
  Serial.print(Second);
  Serial.println(F("  Seconds"));
};

Discussions

rodrig.ky wrote 06/18/2019 at 06:55 point

Awesome work! Tested it on my ESP8266

  Are you sure? yes | no

203MadTom wrote 01/24/2018 at 14:47 point

Can someone explain the math in this code? I would like to understand a little more on how it works. Thanks!

  Are you sure? yes | no

ahpigsy wrote 12/30/2017 at 06:23 point

I have this code on several of my boards. On an ESP8266, a Dragino Lora board and an Arduino Mega. On the 8266 it works faultlessly so far, but on the other 2 boards I have the same problem as Dave above, where the day rollers over after about 6 hours.

The only difference I can see in my code is that the ESP8266 uses a delay(2000); in the code for other purposes.   Just wondering if the fix for the other 2 is just extending the delay on them.

  Are you sure? yes | no

Michael Ratcliffe wrote 03/13/2017 at 22:19 point

Hey, sorry about the late reply. 

This is because we have set "long Day=0;". longs only have a full number value, so when it calculates the days anything above 0.5 of a day is classed as one day. 

Or at least I think this is the problem, you can test this by changing long Day=0;  to float Day=0;

  Are you sure? yes | no

Michael Ratcliffe wrote 03/13/2017 at 22:19 point

Hey, sorry about the late reply. 

This is because we have set "long Day=0;". longs only have a full number value, so when it calculates the days anything above 0.5 of a day is classed as one day. 

Or at least I think this is the problem, you can test this by changing long Day=0;  to float Day=0;

  Are you sure? yes | no

Dave Sutherland wrote 03/07/2017 at 17:48 point

Are testing this code on an arduino uno,need it to show uptime for a meat drier project.

Only changed serial.print to lcd.print as I am using a LCD to test with.

Hours,minutes & seconds keep time pretty well but the day increments after less than 10 hours.

tried removing the rollover part of the code with similar result.

Can anyone explain why this is happening and how to get day to increment after 24 hours ?

Thanks

  Are you sure? yes | no