0

I want to use the following function to display MCU's uptime, but after several hours I get error.

void uptime(char *ret_clk)
{
 byte days = 0;
 byte hours = 0;
 byte minutes = 0;
 byte seconds = 0;
 int sec2minutes = 60;
 int sec2hours = (sec2minutes * 60);
 int sec2days = (sec2hours * 24);
 unsigned long time_delta = millis() / 1000UL;
 days = (int)(time_delta / sec2days);
 hours = (int)((time_delta - days * sec2days) / sec2hours);
 minutes = (int)((time_delta - days * sec2days - hours * sec2hours) / sec2minutes);
 seconds = (int)(time_delta - days * sec2days - hours * sec2hours - minutes * sec2minutes);
 sprintf(ret_clk, "%01dd %02d:%02d:%02d", days, hours, minutes, seconds);
}

after 2:55 hours I got a correct answer : 0d 02:55:27

also after 4 hours, a correct answer : 0d 04:06:44

but roughly after 12 hours i get an error: 1d 02:01:27

and after roughly 20 hours I get : 2d 23:74:26

What is wrong in uptime ?

asked May 3, 2021 at 12:53

2 Answers 2

1

On an 8-bit Arduino an int can only hold values in the range -32768 to +32767. One day's worth of seconds is 86400, which is far outside that range.

You should replace your sec2... variables with literals, maybe #defined - and don't forget to force the length for longer values:

#define SEC2MINUTES 60
#define SEC2HOURS 3600
#define SEC2DAYS 86400UL
answered May 3, 2021 at 12:58
3
  • what is the bennefit define it using #define ? whould define as usgined long save the problem ? Commented May 3, 2021 at 13:04
  • 1
    @Guy.D If you put it in a variable and use that variable to calculate other variables to do your calculations with it has to calculate them every single iteration. By using #define (or you could use const unsigned long if you wish) it calculates it at compile time (which I shortcut by just calculating it myself). Commented May 3, 2021 at 13:06
  • what might be that cause getting this result :upTime[0d 10:123:33] ? code was corrected as you suggested Commented May 5, 2021 at 16:13
3

The millis function increments rapidly, hence it easily exceeds the maximum value of int - -32768 to +32767.

So, use unsigned long data types for millis and other stuff that's large.

4 billion and some change is the max value that unsigned long data types can store, hence no overflowing till about 49 days.

answered May 3, 2021 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.