user48730: 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 ?

user4143: 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


user76576: 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.

48730: <p>I want to use the following function to display MCU's uptime, but after several hours I get error.</p>
<pre><code>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, &quot;%01dd %02d:%02d:%02d&quot;, days, hours, minutes, seconds);
}
</code></pre>
<p>after 2:55 hours I got a correct answer : <code>0d 02:55:27</code></p>
<p>also after 4 hours, a correct answer : <code>0d 04:06:44</code></p>
<p>but roughly after 12 hours i get an error: <code>1d 02:01:27</code></p>
<p>and after roughly 20 hours I get : <code>2d 23:74:26</code></p>
<p>What is wrong in <code>uptime</code> ?</p>
 4143: <p>On an 8-bit Arduino an <code>int</code> can only hold values in the range -32768 to +32767. One day's worth of seconds is 86400, which is far outside that range.</p>
<p>You should replace your <code>sec2...</code> variables with literals, maybe <code>#define</code>d - and don't forget to force the length for longer values:</p>
<pre class="lang-cpp prettyprint-override"><code>#define SEC2MINUTES 60
#define SEC2HOURS 3600
#define SEC2DAYS 86400UL
</code></pre>
 76576: <p>The millis function increments rapidly, hence it easily exceeds the maximum value of int -  -32768 to +32767.</p>
<p>So, use unsigned long data types for millis and other stuff that's large.</p>
<p>4 billion and some change is the max value that unsigned long data types can store, hence no overflowing till about 49 days.</p>
