Arduino Delay Without delay()

contrem · January 14, 2020

Delaying actions in Arduino without using the delay() function.

Why not use delay()

Using delay() stops the entire program. For simple programs that might be ok, but if you need to read sensors or buttons, using delay() might cause you to miss events. Since delay() stops the entire program, it is unable to react to any inputs.

What’s the alternative

Instead of using delay() you should compute the desired pause duration using a time function like millis(). Record the time at the start point, and then continuously check to see if the desired duration has passed. An example using this method is available on arduino.cc.

This method quickly gets ugly as soon as you need more than one delay duration. So instead, use the arduino-timer library.

The blink example on arduino.cc becomes trivial using the arduino-timer library:

#include <timer.h>

auto timer = timer_create_default(); // create a timer with default settings

bool toggle_led(void *) {
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // toggle the LED
  return true; // keep timer active? true
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // set LED pin to OUTPUT

  // call the toggle_led function every 1000 millis (1 second)
  timer.every(1000, toggle_led);
}

void loop() {
  timer.tick(); // tick the timer

  // other code goes here without worrying about being blocked by delay()
}

More examples

There are more examples available in the arduino-timer github, including blinking using microseconds and handling multiple delay durations.

Twitter, Facebook