How Do I Blink Without delay()?
Use millis to track time and toggle the LED when enough time has passed.
const int ledPin = 13;
unsigned long last = 0;
const unsigned long interval = 500;
bool state = false;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
if (millis() - last >= interval) {
last = millis();
state = !state;
digitalWrite(ledPin, state);
}
}Why this helps
The loop keeps running, so you can read buttons and sensors at the same time.
Bottom line
millis lets you blink and still do other work.
Related: Why should I avoid delay()? · How do I do multiple things at once? · How do I make a timer?