How Do I Make a Timer?

Use millis to measure elapsed time and run an action when the interval is reached.

timer_example.ino
cpp
unsigned long start = 0;
const unsigned long interval = 2000;

void loop() {
  if (millis() - start >= interval) {
    start = millis();
    // run timed action
  }
}

This creates a repeating timer that fires every two seconds.

Bottom line

millis timers are simple and do not block the loop.

Related: Why should I avoid delay()? · How do I do multiple things at once? · How do I blink without delay?