Millis()?

Hello,

I am new to arduino and am following a book right now.
1 project is a traffic light control system.
If a button is pressed and at least 5 seconds have gone by, the lights will have to change.
Before looking at the code I just made a simple incrementing counter that would reset to 0 every time the lights change.
However looking at the code it uses millis();
From here: millis() - Arduino Reference , I see that it keeps track of the time since the start of the program.
In the code once (millis - counter )> 5000, the lights change.
In the function that changes the lights counter = millis.

This makes sense to me, but I noticed that even though I put 5000 (5 sec), I noticed it was actually shorter than 5 seconds.
Why is that?

Thanks,

How sort short? It can be because the place where you start or end the time counter. It can be because of the precision of the crystal. Or it can be something else that I'm not thinking about.

EDIT: short, not sort!

you may find it easier to think about it as a time stamp...

when last I checked in, this was the time:

lastTime = millis();

I wonder how much time has passed since last time I checked in?

millis() - lastTime = elapsedTime

I wonder if 10seconds has transpired since last time I checked:

if (millis() - lastTime >= 10000UL) // millis() is an unsigned long, fyi

Also, be careful with wrap around. Millis() returns an unsigned long. That means it has a maximum size o 4,294,967,296. So dividing by 1000 milliseconds per second, then 60 seconds per minute, then 60 minutes per hour, then 24 hours in a day, leaves us with 49.710269 days.

So, once every 49.71 days the millis() value will wrap around from a very large number (4,294,967,295) to 0. So your logic must handle that case or something very odd will happen on the 49th day.

bnicholson:
Also, be careful with wrap around.

If "lastTime" is an unsigned long, that will not be a problem at all :wink:

http://playground.arduino.cc/Code/TimingRollover