Arduino - LED - Blink Without Delay

by DIYables in Circuits > Arduino

13 Views, 0 Favorites, 0 Comments

Arduino - LED - Blink Without Delay

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://arduinogetstarted.com/tutorials/arduino-led-blink-without-delay

This build blinks an LED on a fixed interval while also watching a button, without using delay(). A delay()-based blink blocks Arduino from checking the button during the wait, so presses get missed. This guide wires up the same LED and button and shows the millis()-based approach that lets Arduino do both tasks without blocking.

Supplies

Wiring

Connect the LED to pin 3 and the button to pin 7. Using an LED module with a built-in resistor simplifies the wiring since you don't need a separate 220Ω resistor on the breadboard. Wire the button so its state can be read as HIGH or LOW on pin 7.

How It Works

With delay(), Arduino turns the LED on or off, then calls delay(1000) before checking the button. Any press that happens during that one-second delay is missed because the processor is stuck waiting. The without-delay version uses millis() to track elapsed time instead of blocking, so the loop keeps running continuously and can check the button on every pass, catching every press while still blinking the LED on schedule.

Code

The delay() sketch toggles ledState, writes it to LED_PIN, then calls delay(BLINK_INTERVAL) before reading the button and printing its state if it changed. The without-delay sketch replaces that blocking delay with a millis()-based timer: it compares the current time against the last time the LED changed, and only toggles the LED once BLINK_INTERVAL has passed, while the button is read every loop iteration regardless of the blink timing. A follow-on example extends this same pattern to blink two LEDs at different intervals while still checking the button.

arduino-led-blink-without-delay.ino - the sketch from the tutorial, unchanged.

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-led-blink-without-delay
*/

// constants won't change:
const int LED_PIN = 3; // the number of the LED pin
const int BUTTON_PIN = 7; // the number of the button pin

const long BLINK_INTERVAL = 1000; // interval at which to blink LED (milliseconds)

// Variables will change:
int ledState = LOW; // ledState used to set the LED

int previousButtonState = LOW; // will store last time button was updated

void setup() {
Serial.begin(9600);

// set the digital pin as output:
pinMode(LED_PIN, OUTPUT);

// set the digital pin as an input:
pinMode(BUTTON_PIN, INPUT);
}

void loop() {
// if the LED is off turn it on and vice-versa:
ledState = (ledState == LOW) ? HIGH : LOW;

// set the LED with the ledState of the variable:
digitalWrite(LED_PIN, ledState);

delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect

int currentButtonState = digitalRead(BUTTON_PIN);

if(currentButtonState != previousButtonState) {
// print out the state of the button:
Serial.println(currentButtonState);

// save the last state of button
previousButtonState = currentButtonState;
}

// DO OTHER WORKS HERE
}

Things That Go Wrong

If you press the button during the delay() version's one-second delay, that press won't show up in the Serial Monitor since the processor isn't reading the pin at that moment. If you see missed presses when testing with four button pushes, you're running the delay() version; switch to the millis() version and all four presses should register.

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://arduinogetstarted.com/tutorials/arduino-led-blink-without-delay