[image] ghoshofx1: I was wondering if delay() could always be replaced by millis() - would that be "optimizing" my code ? Optimize for what? Your code probably not became faster or shorter with millis(). But if you need that your program be able to do something else while the waiting for del… Answer from b707 on forum.arduino.cc
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Alternatives to delay() - General Guidance - Arduino Forum
August 10, 2023 - hi, just built my first project with Arduino. Was hoping to optimize it in a few ways (reduce global variables, lesser string variables etc.) when i stumbled upon a few posts saying that delay() was for beginners and that millis() was more useful when multi-tasking... although using either ...
🌐
DigiKey
digikey.com › en › maker › tutorials › 2022 › how-to-avoid-using-the-delay-function-in-arduino-sketches
How to Avoid Using the Delay() Function in Arduino Sketches
February 23, 2022 - Instead of using delay(), you can employ millis() in your sketch and check how much time has elapsed since the Arduino last executed some code. Alternatively, you can also utilize a custom counter and increment it in the loop()-method.
Discussions

delay, but without delay.
So I was doing some research online, and I looked at "while", and I believe that it is a suitable replacement for delay, without pausing everything else on the board while(millis() More on forum.arduino.cc
🌐 forum.arduino.cc
19
0
June 24, 2020
arduino uno - Alternative to delay function - Arduino Stack Exchange
I am trying to make a workaround for the delay function. I am trying to use the function in the Analog Write Mega example, by replacing delay with the function i made. However, whenever i use my fu... More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
Alternative to delay() function Library
Hi guys! I know few of us have been using the delay() function and encountered some problems especially when using Interrupts. I could not say that I'm really good at Arduino. But from my experience, using the delay() function while expecting for an input from a sensor or interrupt buttons ... More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
June 14, 2020
Delay Function Alternatives
Hi, I noticed that most advance programmers tend to not use the delay function. If they don't use delay, what do they use to lets say delay the code for a hour? More on forum.arduino.cc
🌐 forum.arduino.cc
12
0
September 2, 2021
🌐
Stack Overflow
stackoverflow.com › questions › 40959004 › arduino-alternative-for-delays
c++ - Arduino alternative for delays? - Stack Overflow
digitalWrite(greenled, HIGH); //Green on for 1 seconds delay(greenDuration); digitalWrite(greenled, LOW); //Green off, yellow on for 1 seconds digitalWrite(yellowled, HIGH); delay(1000); digitalWrite(yellowled, LOW); //yellow off, red on for 1 seconds digitalWrite(redled, HIGH); delay(1000); digitalWrite(redled, LOW); //Red off digitalWrite(greenled2, HIGH); //Green on for 1 seconds delay(1000); digitalWrite(greenled2, LOW); //Green off, yellow on for 1 seconds digitalWrite(yellowled2, HIGH); delay(1000); digitalWrite(yellowled2, LOW); //yellow off, red on for 1 seconds digitalWrite(redled2, HIGH); delay(1000); digitalWrite(redled2, LOW); //Red off
🌐
Arduino Forum
forum.arduino.cc › projects › programming
delay, but without delay. - Programming - Arduino Forum
June 24, 2020 - So I was doing some research online, and I looked at "while", and I believe that it is a suitable replacement for delay, without pausing everything else on the board while(millis()
🌐
DZone
dzone.com › data engineering › iot › arduino: using millis() instead of delay()
Arduino: Using millis() Instead of delay() - DZone
November 20, 2019 - With millis(), we can ensure that the loop runs as often as we want, regardless of the execution time (obviously, as long as the execution time is less time the desired period). With delay(), this is not possible since we do not know how long ...
🌐
Arduino Forum
forum.arduino.cc › projects › showcase
Alternative to delay() function Library - Showcase - Arduino Forum
June 14, 2020 - Hi guys! I know few of us have been using the delay() function and encountered some problems especially when using Interrupts. I could not say that I'm really good at Arduino. But from my experience, using the delay() f…
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › general guidance
Delay Function Alternatives - General Guidance - Arduino Forum
September 2, 2021 - Hi, I noticed that most advance programmers tend to not use the delay function. If they don't use delay, what do they use to lets say delay the code for a hour?
🌐
DeepBlue
deepbluembedded.com › home › blog › arduino delay() function tutorial
Arduino delay() Function Tutorial
July 26, 2025 - The best alternative for delay is using internal timers instead. The timer module will provide a time base that can be easily checked and used to trigger various logical functions in your applications.
🌐
Reddit
reddit.com › r/arduino › alternative for delay(100)
r/arduino on Reddit: Alternative for delay(100)
May 4, 2022 -

I have a bit of code that is using Delay(100), and it’s causing a bit of problem my dice roller. Is there an alternative to delay to keep the flame like flicker that delay is giving me?

Top answer
1 of 2
4

Real embedded systems outside the artificial world of Arduino pretty much never use busy-delays/busy-wait loops as a means to control timing. Such busy-waits are deeply problematic since they stall program execution and cause 100% CPU utilization with 100% current consumption, for no good reasons.

Alternatives used in a professional context involve hardware timers. Either the dedicated timer hardware peripherals, or a dedicated real-time clock hardware peripheral. These can be used either by polling a flag or by interrupts.

For controlling LEDs specifically, "output compare" features of a timer peripheral can be used to automatically activate/deactivate a GPIO pin when a timer runs out. PWM features are also very common, particularly for reducing current consumption or to enable multiple colors in case of RGB.

In either case, any of these various hardware features allow the LED to be controlled in a concurrent manner at the side of the main program.

2 of 2
1

Whether you're using a busy for loop or the busy-wait delay() function, both are bad.

Study Blink without delay: https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay

For more advanced cooperative multitasking techniques, see my answer here: How to do high-resolution, timestamp-based, non-blocking, single-threaded cooperative multi-tasking

For reading the RC PWM control signal, that's complicated, but I have an old example here: https://github.com/ElectricRCAircraftGuy/PWM_Reader2_WORKS_PERFECTLY_Hayden_car_lights.

Here's the core code. It blinks an LED at a rate proportional to the throttle setting.

YouTube video:

🌐
Arduino Forum
forum.arduino.cc › projects › programming
What is a simple alternative to delay? How would it look in my code? - Programming - Arduino Forum
March 11, 2020 - Hi I am very much a newbie at this I have been using delay for my code but it is causing problems for executing keyboard presses. I am trying to find a simple alternative. I have tried Millis and it didn't work well for …
🌐
Arduino Forum
forum.arduino.cc › projects › programming
A function to replace delay()? - Programming - Arduino Forum
January 17, 2018 - Like most newbies, I'm trying to get my head around millis() in a consistent way. I get the basic gist, but writing all the steps out every time I want to use delay() is tedious. So I wondered if I could write a function…
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › time › delay
delay() | Arduino Documentation
For alternative approaches to controlling timing, see the Blink Without Delay sketch, which loops, polling the millis() function until enough time has elapsed. More knowledgeable programmers usually avoid using · delay() for timing events longer than 10s of milliseconds, unless the Arduino ...
🌐
Programming Electronics Academy
programmingelectronics.com › home › arduino sketch with millis() instead of delay()
millis() instead of delay() with Arduino [Guide + Code]
November 13, 2023 - Trying to use millis() instead of delay() with Arduino? This lesson will give you the explanations and code you need to master millis()!
🌐
Arduino Forum
forum.arduino.cc › international › deutsch
Alternative zum delay - Deutsch - Arduino Forum
March 18, 2020 - Hi Forum :slight_smile: ich habe wohl schon gemerkt, dass es sehr wichtig ist, an welchen Stellen man delays benutzt. Besonders wenn man möchte, das Sensoren anständig und kontinuierlich Ihre Werte messen. Jetzt stehe …
🌐
Adafruit
learn.adafruit.com › multi-tasking-the-arduino-part-1 › ditch-the-delay
Ditch the delay() | Multi-tasking the Arduino - Part 1 | Adafruit Learning System
November 3, 2014 - Sweep uses the delay() to control the sweep speed. If you try to combine the basic blink sketch with the servo sweep example, you will find that it alternates between blinking and sweeping. But it won't do both simultaneously.