How to Create Non Blocking Delay in Arduino
To create a
non blocking delay in Arduino, use the millis() function to check elapsed time without stopping the program. This lets your code run other tasks while waiting, unlike delay() which pauses everything.Syntax
The basic syntax for a non blocking delay uses millis() to track time:
unsigned long previousMillis = 0;stores the last time an action was done.unsigned long interval = 1000;sets how long to wait in milliseconds.if (millis() - previousMillis >= interval)checks if the wait time has passed.- Inside the
if, updatepreviousMillis = millis();and perform your action.
arduino
unsigned long previousMillis = 0; unsigned long interval = 1000; // 1 second void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Your code here } }
Example
This example blinks the built-in LED every second without stopping other code from running. It uses millis() to check time instead of delay().
arduino
const int ledPin = LED_BUILTIN; unsigned long previousMillis = 0; unsigned long interval = 1000; // 1 second void setup() { pinMode(ledPin, OUTPUT); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; digitalWrite(ledPin, !digitalRead(ledPin)); // toggle LED } // Other code can run here without delay }
Output
The built-in LED on the Arduino board blinks on and off every second without pausing other code.
Common Pitfalls
Common mistakes when using non blocking delay include:
- Not updating
previousMillisinside theifblock, causing the action to repeat rapidly. - Using
delay()inside the loop, which blocks other code. - Not using
unsigned longfor time variables, which can cause errors whenmillis()overflows after ~50 days.
arduino
/* Wrong way: missing update of previousMillis */ unsigned long previousMillis = 0; unsigned long interval = 1000; void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // previousMillis = currentMillis; // Missing update causes rapid repeats // Action here } } /* Right way: update previousMillis */ void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Action here } }
Quick Reference
Tips for using non blocking delay in Arduino:
- Use
millis()to track elapsed time without stopping the program. - Always update your time marker variable after the action.
- Keep time variables as
unsigned longto handle overflow safely. - Use this method to keep your Arduino responsive to inputs and sensors.
Key Takeaways
Use millis() to measure elapsed time for non blocking delays.
Always update the previous time variable after the delay interval passes.
Avoid delay() to keep your Arduino program responsive.
Use unsigned long for time variables to handle millis() overflow.
Non blocking delay lets your code do other tasks while waiting.