Can millis() be stored in a float?

I am using millis() to count off some things by storing it to a float variable. I am able to re-boot the board every 30 days. Will this work? I am not sure from all I read.

In other words the float variable stores the value of millis() after a certain sensor is triggered. After 30 days the whole board is rebooted.

Thank you in advance.

The number of milliseconds since the processor booted is an integer.
Why cripple the value by storing in a float?

I am using millis() to count off some things by storing it to a float variable.

Why ?

Can millis() be stored in a float? -- Yes.

Will it always work like you expect? -- Who knows?

i am trying to keep track of when a sensor is triggered and compare it to its prior triggering: the prior time may be moments or weeks ago. So comparing the millis() now to the last time is my goal.

As long as you're not talking about more than about seven weeks, an unsigned long will do fine. Way better than a float.

After that, simple ways of extending the period can be implemented. It seems unlikely that millisecond accuracy will be required.

Always use 'unsigned long' for all millis() timing.

The millis() timer will roll over every ~1193 hours no matter how you store it's return value.

If you need to time an interval longer than that, you could check for roll over and increment another variable. Or, use an RTC.

SamBrownADK:
i am trying to keep track of when a sensor is triggered and compare it to its prior triggering: the prior time may be moments or weeks ago. So comparing the millis() now to the last time is my goal.

So why use a float ?

Another problem is that the usual trick of using subtraction on 'unsigned long' variables to get around the rollover characteristic won't work with floats.

I am using float 'cause I thought it would be a variable capable of a big number. No other particular reason. If it is not the best choice then from what I read here, unsigned long will be superior. i am not sure why yet (I have to read more about the two).

A float is 32 bits. It has a range of about +/- 1038
An unsigned long is also 32 bits, but with a range of about 0 to 4x109

How do they do that?

SamBrownADK:
i am trying to keep track of when a sensor is triggered and compare it to its prior triggering: the prior time may be moments or weeks ago.

If you use seconds instead of milliseconds the math is the same and the window is expanded to...

(2^32)/86000/365.2425 = ~136 years

Is seconds a reasonable granularity?

Will anyone care if the delta is greater than 136 years?

Edit: Days per year corrected.

You have quite short years. :slight_smile:
(The right answer is ~136 years.)

just use a $3.00 RTC and the Time Library

#include <TimeLib.h>

and regular Unix timestamps...

oqibidipo:
You have quite short years. :slight_smile:

Ugh. Well, close enough.

(In my defense I've been watching hours of MIT 6.006 videos. My brain has obviously reached its limit.)