I need a more accurate millis()

I'm using the "blink without delay" trick to drive a stepper motor
all working very nicely
however, the minimum resolution is 1 mS

is there a "more accurate" way of doing this?

obviously millis() will only return whole milliseconds
I'd ideally like micros() to not rollover as it does every 70 minutes or so

alternatively, how does one handle the micros() rollover?

TIA (as da yoof say!)

What's the problem with using micros?
millis rolls over too, but that is easily coped with.

Well you could always make your own, but before going there, let me ask, are you saying that you need to time an event that lasts longer than 70 minutes down to microsecond accuracy? That's 0.24 parts per billion if I did the maths right. On 16MHz boards, micros() has a resolution of 4µs, so that'd still be 1ppb.

I'd ideally like micros() to not rollover as it does every 70 minutes or so

Why is this a problem? Do you plan to step only once an hour or less?

alternatively, how does one handle the micros() rollover?

The same as the millis() rollover. Step more than once per 70 minutes.

er
I may be slightly confused (normal state some say) but...

I don't want to step once every 70 minutes, but I want to continue stepping past 70 minutes
now, up to 70 minutes micros() returns a monotonically increasing number

but if I start my measurement just before the 70 minutes is up

startTime = micros();

and I want to wait 1.5 mS (1500 microseconds),
then the test

if (micros() - startTime > 1500)

will never happen

and I want to wait 1.5 mS (1500 microseconds),
then the test

if (micros() - startTime > 1500)

will never happen

Would you like to place a small wager on that? Subtraction involving unsigned longs is guaranteed to work, as long as the value has not rolled over more than once.

mmcp42:
er
I may be slightly confused (normal state some say) but...

I don't want to step once every 70 minutes, but I want to continue stepping past 70 minutes
now, up to 70 minutes micros() returns a monotonically increasing number

but if I start my measurement just before the 70 minutes is up

startTime = micros();

and I want to wait 1.5 mS (1500 microseconds),
then the test

if (micros() - startTime > 1500)

will never happen

Your intuition is wrong, it will indeed happen exactly when you want it to. micros() returns an unsigned long, so the maths will be done modulo 2^32. You should declare startTime as "unsigned long".

will never happen

Yes it will. You don't yet understand the greatness (not magic) of using subtraction in context with the rollover.

Ah
Looks like I have some testing to do!
Thanks peeps

quick update
changed the code to use micros() instead of millis()
obviously used bigger numbers

have to report it's working as you all predicted!
well I learnt something today

thanks all :slight_smile:

Great news, it doesn't get much better than that! :slight_smile:

The Arduino does also support the long long datatype aka uint64_t . This is a 64 bit integer, and you could make a micros() timer that increases the uint64_t.

You still should handle the micros() overflow you you got a counter that can count up to 2^64-1 = 18446744073709551615 micros =~ 584942 years.

Warning doing Math with long long is time intensive.

Maybe you better use the bignumber class port of Nick Gammon - http://arduino.cc/forum/index.php/topic,85692.0.html -

Warning doing Math with long long is time intensive.

But it probably won't take 584942 years. :slight_smile:

tempting though it is to build a system that would run for half a million years
I suspect something else may give up before that
I include myself in that list
in fact, pretty near the top of the list!

How about a 10,000 year clock?

closer, but no 'duino interface! :slight_smile:

tempting though it is to build a system that would run for half a million years
I suspect something else may give up before that
I include myself in that list
in fact, pretty near the top of the list!

There is allways cryogenics ... - xkcd: Cryogenics -

Need one heck of a battery tho :wink:

Need one heck of a battery tho

Not one of those little 9V rectangular jobs? :stuck_out_tongue:

hee hee
battery to last 10k years?
or battery to power the cryogenics!

hmm not sure I want to try Arduino controlled cryogenics just yet
(image of hand thawing out, reaching for the reset button...)