# millis to minutes and hours

Is calculation of millis to minutes right ? Sometimes I`m getting different values for minutes 1:26 then after a second 1:48 then again something new.

``````#define seconds_per_minute 60

#define ms_per_minute 60000UL

#define mintues_per_hour 60

void setup() {

Serial.begin(9600);
}

void loop() {
_time(1);
_time(0);
}

void _time(bool a) {

static unsigned long uptime = 0;

if (a) {
uptime = millis();
return;
}

Serial.print(F("hr:"));

Serial.println((uptime / ms_per_minute) / mintues_per_hour);//hours

Serial.print(F("min"));

if ((uptime / ms_per_minute) > 59) Serial.println((uptime / seconds_per_minute) % seconds_per_minute); //milliseconds to minutes

else Serial.println(uptime / ms_per_minute);

delay(1000);

return;
}
``````

thanks.

What are you trying to achieve here? Your logic confuses me.

Trying to understand where i went wrong. I need to print elapsed time after arduino started in hrs and minutes and print it.

But first uptime var must be updated and then at second request printed. This is part of a big project so i narrowed problem down to this conversion.

What does this program you gave us actually print to Serial? What did you expect it to print?

You are allowed to use delay() to slow it down a little.

This looks like a typo...

``````(uptime / seconds_per_minute)
``````

At any time,

``````#define ms_per_hour  3600000
#define ms_per_min    60000
#define ms_per_sec    1000

now = millis();

byte hour = (now / ms_per_hour);
now -= (hour * ms_per_hour);
byte minute = (now / ms_per_min);
now -= (minute * ms_per_min);
byte second = (now / ms_per_sec);
``````

#define mintues_per_hour 60

Shouldn't that be "minutes"?

It's either a small Tuesday or toothpaste-flavoured. 8)

@FredScuttle
Good catch! :-))))

I want to do without modifying or creating new variable.
Think solved that:
(uptime/ms_per_min)/minutes_per_hour// this way im getting hours

Then:
(uptime/ms_per_min)/minutes_per_hour-(((uptime/ms_per_min)/minutes_per_hour)xminutes_per_hour)

Example:90mins=1.5hr=1hr and 30mins
(5400000/60000)/60=1.5hr fraction is dropped so 1hr
(5400000/60000)/60-(((5400000/60000)/60)x60) =30mins

I want to do without modifying or creating new variable.

Why? Variables are cheap. Use as many as you need to write understandable, debugable code.

Then what comments are for?

surepic:
Then what comments are for?

Explaining the bone-headed (or absolutely brilliant) reason(s) for writing the code that way.

Comments are useless for debugging. You can't set a breakpoint and examine a comment.

Well, you can't set breakpoints and examine values on the Arduino, either, but that's neither here nor there.

Comments are to explain the "why" of the code. The code itself explains the "what" and the "how". But often you find yourself asking "Why does it do that?" so the comments help.

Just repeating what was in the code is a bad comment for two reasons: anyone who knows C can read what it does without the comment and the code changes, often leaving the comment unchanged and therefore misleading.

For example:

``````#define DelayTime 500 //delay for 5 seconds
#define DelayTime 500 //milliseconds. Sets the speed of the print output
``````

Which comment is wrong? Which comment helps you understand the effect of this line on the rest of the program?

Makes sense. Were pretty informative posts.
DKWatson +1 used your code.

If you like using millis to calcolate hours you have to know that missis is not a good timer, it xan miss 0_5%, better is use a RTC

Cheers.

Silente:
If you like using millis to calcolate hours you have to know that missis is not a good timer, it xan miss 0_5%, better is use a RTC

Your speel cheker nees werk.

Im using it as uptime indicator. Like how long system is up.

``````  if ((uptime / ms_per_minute) > 59) Serial.println((uptime / seconds_per_minute) % seconds_per_minute);
else Serial.println(uptime / ms_per_minute);
``````

Translation:

If we have been up for at least an hour, then print out the number of miliseconds we have been up divided by 60 - but modulo 60000, so if we have been up for 60 hours then start again from zero.

else, (we have not been up for at least an hour), print out the number of minutes we have been up.

That `uptime / seconds_per_minute` doesn't really make any sense, since uptime is in milliseconds.

``````int days = uptime/milliseconds_per_day;
int hours = (uptime/milliseconds_per_hour) % hours_per_day;
int minutes = (uptime/milliseconds_per_minute) % minutes_per_hour;
int seconds = (uptime/milliseconds_per_second) % seconds_per_minute;
int milliseconds =  (uptime/milliseconds_per_millisecond) % milliseconds_per_second;
``````

Alternatively:

``````long t = uptime;
int ms = t % 1000;
t /= 1000;
int seconds = t % 60;
t /= 60;
int minutes = t % 60;
t /= 60;
int hours = t % 24;
t /= 24;
int days = t;
``````

@PaulMurray
(Uptime/mspermin)%minutesperhour

This was exactly what i was trying to achieve!

Got a little messed up with modulo i think.

You can also use floor() function for calculation 