Millis/developers

From Wiring

Jump to: navigation, search
This page is intended as a reference for core developers only, and the content is only changed after a consensus has been reached through discussion of any such changes. Please use the Discussion page to talk about any changes or issues you have with the current implementation.
millis()
Classification Delay and Timing Control
Suggested location WDelay.h
C++ required No
Timing constraints None
Version introduced 1.0 (0100)
Framework dependencies None
Prototype(s)
uint32_t millis()
Required? Yes
Issues None


Contents

Description

Returns the number of milliseconds (thousandths of a second) since starting an application. This information is often used for timing sequences.

Black Box Input

No input.

Black Box Output

Long. The number of milliseconds since starting the application.

Overloading

None. Single signature only.

Notes

There are 1000 milliseconds in a second.

Because millis() returns a long integer (32 bits), the value will "roll-over" (overflow) and go back to zero after approximately 50 days. Here's why:

2^32 = 4,294,967,296 (largest number for a long int)
/ 1000 = 4,294,967.296 (seconds)
/ 60 = 71,582 (floor number of minutes)
/ 60 = 1,193 (floor number of hours)
/ 24 = 49.7 (days)

But differential math on millis() will always result in correct calculations. For example:

start  = millis() = 0xfffffff0 (4,294,967,280 - just before overflow)
finish = millis() = 0x00000012 (12 - just after overflow)
finish - start = 0x00000012 - 0xfffffff0
               = 0x00000012 + (0x00000010) (two's complement)
               = 0x00000022

Which yields the correct result. This works for single "roll-over" only.

Proposed Changes

Personal tools