Go Down

Topic: Usimg millis() as a global timer (Read 575 times) previous topic - next topic

fiddler

I want to use millis() as a global timer, however when it rolls over to zero, how do I handle that ?


Cheers
K

Coding Badly


What do you mean by "global timer"?

dxw00d

You could search the forum. There have been many threads on the subject. There is also an article on the playground.

fiddler

I did search the site, but obviuosly not deep enough :-)

Found the answer here
http://www.arduino.cc/playground/Code/TimingRollover

Cheers
K

GoForSmoke

Use unsigned longs to track time and use a math operation in the compare.

if ( now - start >= desired_interval ) {
  /* do whatever */
}

You won't have rollover problems, the answer will be positive and correct up to the unsigned long limit.

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up