[SOLVED] millis()

How long can you reference against millis() ? I am using it and the code seems to stop after a minute.

fliggygeek: How long can you reference against millis() ? I am using it and the code seems to stop after a minute.

You mean using it before your code goes non-responsible? Indefinitely. The value DOES reset after 2^32 milliseconds, which is about 49.7 days.

I suspect there is other problems in your code that stopped it from working beyond a minute. Why not sharing it here so some of us can look at it?

fliggygeek: How long can you reference against millis() ? I am using it and the code seems to stop after a minute.

Post your code, as millis() doesn't do anything dramatic after a few minutes.

Lefty

Cramming millis() into an unsigned integer means it will go screwy in little over a minute (65.536 seconds to be precise). All the variables relating to millis() must be defined as the unsigned long type.

pluggy: Cramming millis() into an unsigned integer means it will go screwy in little over a minute (65.536 seconds to be precise). All the variables relating to millis() must be defined as the unsigned long type.

This is probably beside the point, but I'm pretty sure that the timer actually counts 1024 milliseconds per real second, so the integer actually overflows after exactly 64 seconds. Again, beside the point, but true.

About the code, pluggy is right. You'll have to define all variables relating to time with the long identifier. If you're crammed for space (Which I doubt you are) you could divide the number of milliseconds by 1000 (Or 1024 to be precise) if you can work with just knowing how many seconds have elapsed.

Thanks, my code was using an unsigned int to store millis() values. When converted to an unsigned long, it now runs for ages. What is the lifetime of an unsigned long?

fliggygeek: Thanks, my code was using an unsigned int to store millis() values. When converted to an unsigned long, it now runs for ages. What is the lifetime of an unsigned long?

If you meant when the value will overflow, it 49.7 days? That is what I've said in my reply, 2^32 milliseconds.

Lifetime And Scoping mean something else. You probably don't need to be concerned about them now.

unsigned longs are 32 bits: 0x0000 0000 to 0xFFFF FFFF = 2^32 = 4,294,967,295 milliseconds * 1sec/1000mS * 1 min/60 Sec * 1 hr/60 min * 1 day/24 hrs = 49.7 days

Cool, thanks.

I'm pretty sure that the timer actually counts 1024 milliseconds per real second

The timer ticks every 1024 microseconds, but the core code painfully converts that to actual milliseconds (which means that every once in a while, a single clock tick increments the millis() return value by 2...)

Oh. I see. Arduino is cool like that, making the millis perfect. :)

This discussion about millis is quite relivent to my rpm control issue. if I'm going to be counting rpm I could run into a huge problem if there are 1024 milliseconds in an an arduino second.

This thread is over five years old; it'll be starting school soon.

Do you think there are 1024 Arduino milliseconds in a second? Have you checked?