Keep a task running while an LED changes
Replace the pauses in Blink with elapsed-time checks, leaving the main loop available for an input or another task.
You needA browser for the timing model. For upload: a compatible board with a documented LED and its supported setup.
Reference checked 9 September 2026. Authored exercise; no physical build result is claimed.
Understand the change
Predict the timeline
Unlike the original Blink, this version starts LOW. Predict transitions at approximately 750, 1000, 1750 and 2000 ms before inspecting the browser timeline.
Replace waiting with checking
loop returns repeatedly. It changes the output only when the elapsed interval has passed, allowing another short task to run between transitions.
Change one interval
Change HIGH to 500 ms while keeping LOW at 750 ms. The full cycle becomes approximately 1250 ms. Do not add delay to make the timing look neater.
Keep a working baseline
If you have compatible hardware, upload the unchanged sketch first, observe it, then make the one change. Save the exact board and observed timing for the input-control stage.
A focused request to try
Use this with a suitable AI assistant. Its response will vary; compare it with the checks below.
Rewrite my Blink sketch using millis and unsigned long elapsed-time subtraction. Start LOW, wait 750 ms, then alternate HIGH for 250 ms and LOW for 750 ms. Do not use delay. Keep LED_BUILTIN. Explain why this lets loop inspect another input and why an active-low LED reverses the visible meaning of HIGH.
Worked reference example
Read the example, predict one change, then check the result.
const unsigned long highMs = 250;
const unsigned long lowMs = 750;
unsigned long changedAt = 0;
bool highState = false;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
changedAt = millis();
}
void loop() {
const unsigned long now = millis();
const unsigned long interval = highState ? highMs : lowMs;
if (now - changedAt >= interval) {
highState = !highState;
changedAt = now;
digitalWrite(LED_BUILTIN, highState ? HIGH : LOW);
}
// Other short, non-blocking work can run here.
}Step through the output timing
Step through a model of the logic. This does not run or validate physical hardware.
| Time | Output level |
|---|
Check your result
- Default transitions begin at 750 ms LOW-to-HIGH and 1000 ms HIGH-to-LOW.
- HIGH 500 ms plus LOW 750 ms gives a 1250 ms nominal cycle.
- No delay call appears in the loop.
Where this can go wrong
This browser timeline models logic, not processor timing. Slow work in loop delays transitions. LED wiring may be active-low; unsigned elapsed subtraction handles normal millis rollover only when checks occur within its wrap interval.
Go to the source
Arduino Blink Without Delay. Confirm current tool instructions before using them.