Before you start
A supported Arduino board, USB data cable and the Arduino IDE.
You will leave with
A non-blocking LED timer you can reuse while adding inputs.
A blinking LED becomes much more useful when the rest of your program stays awake. This exercise turns the built-in LED into a status light that can share the main loop with future sensors and controls.
This is an original learning exercise based on documented Arduino behavior. The sketch has not been physically bench-tested for this publication. Start with a compatible board and its built-in LED before attaching any external hardware.
What you need
- An Arduino Uno or another board with a supported LED_BUILTIN definition.
- A data-capable USB cable and the Arduino IDE.
- A few minutes to change one setting at a time and record the result.
The built-in LED avoids extra wiring. If you later use a separate LED, select a series resistor and observe the board's pin-current limits; never connect an LED directly across an output and ground without current limiting.
1. Upload a small, explicit timer
Select your board and port in the IDE, then upload this sketch. The interval is measured in milliseconds. A 500 ms interval means the light changes state twice per second, producing one complete on-and-off cycle per second.
const unsigned long intervalMs = 500UL;
unsigned long lastChangeMs = 0;
bool ledOn = false;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop() {
const unsigned long now = millis();
if (now - lastChangeMs >= intervalMs) {
lastChangeMs = now;
ledOn = !ledOn;
digitalWrite(LED_BUILTIN, ledOn ? HIGH : LOW);
}
// Add other short, non-blocking tasks here.
}2. Read the loop as a repeated question
Each pass asks whether enough time has elapsed. If the answer is no, execution moves on immediately. If the answer is yes, the sketch records the current time and changes the LED state. There is no waiting room inside this logic: the processor can keep checking other work between changes.
Notice that the program stores a state, not just a timestamp. The timestamp answers when the last change happened; the Boolean answers whether the next write should turn the light on or off. Giving these responsibilities separate names makes later changes easier to understand.
3. Try two deliberate changes
First change the interval to 1000UL. Before uploading, predict the full cycle time. Then try 100UL. Write down whether your prediction matched what you saw. This simple habit catches unit mistakes before they migrate into a motor-control program.
Next add a second timed activity, such as printing a short status message once every two seconds. Give that activity its own last-run timestamp and interval. Do not print on every pass through loop: a busy serial connection can introduce blocking and obscure the behavior you are trying to learn.
Why subtraction matters
The elapsed-time expression subtracts the previous unsigned timestamp from the current one. Arduino's millis counter eventually wraps. For ordinary short intervals, unsigned subtraction handles that wrap correctly; comparing now against lastChangeMs plus the interval is a less robust pattern. Keep timing variables unsigned and keep intervals far shorter than the counter's full range.
This version schedules the next change relative to the most recent actual change. That is a good fit for a status LED. It is not a precise sampling clock, and a long operation elsewhere in the loop can still delay it. Non-blocking timing works only when the surrounding work also returns promptly.
If the light does not behave
- Confirm the upload succeeded and the selected port belongs to the board you are watching.
- Check your board documentation: some built-in LEDs use different hardware or inverted logic.
- Look for a remaining delay call or a loop that waits forever for input.
- Return to this small sketch before debugging newly added hardware.
Your finished result
You now have a reusable status-light pattern. For a robot, the next useful step is a second independent task that reads a button, while leaving the LED timer unchanged. Keep movement disabled until inputs behave predictably.
Primary reference: Arduino's Blink Without Delay documentation.
Reference exercise · Read the evidence and checking record.