BT San Technology

2 April 2026

delay versus millis: writing Arduino code that handles several jobs without blocking

2 min read · Automation

Illustration: a push button, three LEDs, a servo, a small board and an oscilloscope showing square waves
AI-generated illustration

delay stops the sketch's flow at the point where it is called, which can leave the program unable to respond to a button or read incoming data in time. Using elapsed time together with a state machine lets the loop come back and check several jobs. But it does not create parallel execution, and it does not meet a deadline by itself. Long-running work inside a library still has to be checked separately.

What this teaches and the order of experiments

  1. Separate the jobs of blinking an LED, reading a button and sending data, and give each job its own time interval.
  2. Calculate the time difference as an unsigned value of a suitable type, to handle the counter wrapping around.
  3. Build the states IDLE, RUN and ERROR, and make the conditions for changing state visible.
  4. Measure the loop time under real communication traffic, and set a timeout at every point that could wait.

Exercise and example applications

Exercise: write a simulation program that reads a button, switches a light in two stages, and sends the status over Serial, so that the button can be pressed while the light is running.

Limits and what else to check

millis cannot replace interrupts, timers or an RTOS in every case. If you need a very short response time, design for it and measure it separately. Do not move this logic into control of a real load while skipping system checks.

Manufacturer documents to read

  • Arduino Hardware
  • Arduino IDE documentation

References

  1. Arduino Hardware (accessed 2026-11-21)
  2. Arduino IDE documentation (accessed 2026-11-21)

Add us on LINE

Scan with the LINE app on your phone

QR code to add LINE @Bankoos

LINE ID: @Bankoos

Open LINE on this computer