0
0
AutocadHow-ToBeginner · 4 min read

How to Use millis() in Arduino for Timing Without Delay

Use the millis() function in Arduino to get the number of milliseconds since the board started running. This helps you measure elapsed time without stopping your program, unlike delay(). You compare the current millis() value with a saved start time to check if a certain time has passed.
📐

Syntax

The millis() function returns an unsigned long number representing milliseconds since the Arduino started running. You use it to track elapsed time by saving the start time and comparing it to the current time.

Example usage:

  • unsigned long startTime = millis(); — saves the current time
  • if (millis() - startTime >= interval) { ... } — checks if the interval has passed
arduino
unsigned long currentTime = millis();
unsigned long startTime = 0;
unsigned long interval = 1000; // 1 second

void setup() {
  Serial.begin(9600);
  startTime = millis();
}

void loop() {
  currentTime = millis();
  if (currentTime - startTime >= interval) {
    // Do something every 1 second
    startTime = currentTime; // reset start time
  }
}
💻

Example

This example blinks the built-in LED every 1 second using millis() instead of delay(). It allows the program to keep running without pausing.

arduino
const int ledPin = LED_BUILTIN;
unsigned long previousMillis = 0;
const long interval = 1000; // interval at which to blink (milliseconds)

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

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (digitalRead(ledPin) == LOW) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
}
Output
The built-in LED on the Arduino board blinks on and off every 1 second continuously.
⚠️

Common Pitfalls

Common mistakes when using millis() include:

  • Not using unsigned long variables to store millis() values, which can cause incorrect timing due to overflow.
  • Using delay() together with millis() can block your program and defeat the purpose of non-blocking timing.
  • Incorrectly comparing times without subtracting the start time, which can cause logic errors.

Always subtract the saved start time from the current millis() value to handle timer rollover correctly.

arduino
/* Wrong way: */
unsigned long startTime = millis();
if (millis() >= startTime + 1000) {
  // This can fail when millis() rolls over
}

/* Right way: */
unsigned long startTime = millis();
if (millis() - startTime >= 1000) {
  // Correct even if millis() rolls over
}
📊

Quick Reference

Remember these tips when using millis():

  • Use unsigned long to store time values.
  • Subtract start time from current time to check elapsed time.
  • Do not use delay() if you want non-blocking code.
  • Handle rollover automatically by subtraction.

Key Takeaways

Use millis() to track elapsed time without stopping your program.
Always store millis() values in unsigned long variables.
Check elapsed time by subtracting the start time from the current millis() value.
Avoid using delay() when you want your program to keep running smoothly.
Subtracting times handles the millis() rollover automatically.