Article & guide
Read the full article
## Make room for the next job A long delay makes a sketch easy to read but postpones everything else in the loop. This project replaces a repeated delay with elapsed-time checks. An LED changes state every half second while a separate message appears every second. Each task has its own clock. ## Components - Classic ESP32-WROOM-32 development board — 1 - LED — 1 - 330 ohm resistor — 1 - Breadboard, jumper wires and USB data cable ## The timing pattern Keep the previous timestamp for each task. On each pass through loop, read millis() once. If the unsigned difference between the current timestamp and the task timestamp reaches its interval, do that task and update its timestamp. Unsigned subtraction handles the millis counter wrapping for these short intervals without a special reset routine. This is cooperative scheduling, not parallel execution. A slow operation elsewhere in loop still delays both checks. Serial output, network calls and sensor libraries may take time. Measure the behaviour of your actual sketch before claiming precise timing. ## Check the independence Watch the LED and serial output together. Change only the heartbeat interval to 2000 milliseconds and upload again. The LED interval should stay unchanged. Then change only the LED interval. Keep those observations in a table with the version of your sketch. The code prioritises a simple, responsive demo: missed time is not replayed as a burst of events. For accurate periodic sampling, you need to define how to handle drift, late work and missed samples before choosing a scheduling strategy. ## Reference Arduino timing pattern: https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/ ## Before you begin This is an educational build plan for a classic ESP32-WROOM-32 development board, not a report of a physically tested prototype. Check the pin labels and documentation for your exact board. Disconnect USB before changing wiring. Keep the circuit at USB/3.3 V levels; do not connect mains power. The AI-generated cover is illustrative and is not a wiring diagram.
1. Connect the LED
With USB disconnected, wire GPIO23 → 330 ohm resistor → LED anode, then LED cathode → GND. Check the board model and pin labels.
2. Give each task its own timestamp
Upload this sketch, then open Serial Monitor at 115200 baud: ```cpp const int ledPin = 23; unsigned long ledAt = 0, reportAt = 0; bool lit = false; void setup() { pinMode(ledPin, OUTPUT); Serial.begin(115200); } void loop() { const unsigned long now = millis(); if (now - ledAt >= 500UL) { ledAt = now; lit = !lit; digitalWrite(ledPin, lit ? HIGH : LOW); } if (now - reportAt >= 1000UL) { reportAt = now; Serial.println("Loop is still running"); } } ```
3. Add a task deliberately
Add a button read without delay. Confirm that pressing the button can be noticed while the LED continues. Keep interrupt handlers and networking out of this first experiment so you can explain the timing clearly.
4. Explain the limit
Describe what happens if one loop operation blocks for three seconds. Both tasks will run late; using millis does not make a blocking operation disappear.
