Skip to main content

hardware.millis()

Returns the current value of the imp’s free-running millisecond timer

Availability

Device

Returns

Integer — the number of milliseconds counted

Description

This method provides access to a free-running millisecond timer: a counter that starts off at 0 when the imp boots up and increments every millisecond. The value returned is a Squirrel integer: a signed 32-bit value. This means that the value will overflow (become negative) after 231ms, or about 25 days, and count back up to zero in 232ms, about 50 days. However, this does means that calculations of the form (a - b) > 0 correctly give the ordering of times a and b even if one or both is negative — provided a and b are less than 25 days apart.

The counter is set to zero on both a cold boot (power-on) and on a warm boot (wake from deep sleep).

For calendar (absolute) time, use one of the Squirrel built-in calendar-time functions: time(), which returns Unix time (integer, UTC seconds since 1 January 1970), or date(), which returns a table containing UTC year, date, month, hours, minutes and seconds values. Both time() and date() use the imp’s RTC timebase, which has a one-second resolution.

Example Code

The following code uses hardware.millis() to measure the length of time a message takes to be sent from the imp to its agent, for the message to be processed, and for the imp to receive a response.