Pages: [1]   Go Down
Author Topic: Usimg millis() as a global timer  (Read 527 times)
0 Members and 1 Guest are viewing this topic.
NZ
Offline Offline
Jr. Member
**
Karma: 1
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Cheers
K
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 207
Posts: 12904
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


What do you mean by "global timer"?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

NZ
Offline Offline
Jr. Member
**
Karma: 1
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Cheers
K
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

Pages: [1]   Go Up
Jump to: