You are on page 1of 4

millis();

Returns the number of milliseconds since the Arduino board began


running the current program. This number will overflow (go back
to zero), after approximately 50 days.

Syntax
millis();

Note: As millis() function uses milliseconds to calculate elasped time since the
Arduino program started running, it needs to store a large value i.e. 60,000 after
1 minute and 3,600,000 after 1 hour! Due to the large value, the data type for
using millis() function must be long. Do note that this data type is not infinite, it
will overflow after approximately 50 days. Unsigned means that all values are
positive, there is no negative values.
For example:

int ledState = 0;
unsigned long previousMillis = 0;
const long interval = 1000;

void setup(){
pinMode(13, OUTPUT);
}

Note: unsigned long is a data type that can store a value from 0 to
4,294,967,295.
Continued:
void loop(){
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {


previousMillis = currentMillis;

if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

digitalWrite(13, ledState);
}
}

Note: This program will cause the LED on pin 13 to blink on and off every second, without using delay() function,
by counting the time since the Arduino program has started. The good thing about substituting millis() function
without using delay() functon is that the main loop is continuously running without pausing, which is needed to
receve input signals.

You might also like