Comments on: Why You Shouldn’t Always Use the Arduino Delay Function https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Sat, 03 May 2025 16:46:57 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: rp https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-1033718 Sat, 03 May 2025 16:46:57 +0000 http://randomnerdtutorials.com/?p=15742#comment-1033718 Hi everyone,

could someone please explain EXACTLY what’s being blocked by ‘delay()’?

I am asking because the following doesn’t make sense:

Recently I started looking into ESP-Now and used the two RNT examples. The
sender sends a message every 2 seconds and the receiver simply displays it. The
receiver had an empty ‘loop()’ so i added a 30 second ‘delay()’ to see what it would look like,
with a ‘Serial.print’ before and after it.

void loop() {
Serial.println(“before delay”);
delay(30000);
Serial.println(“after delay”);
}

The result was that the receiver was still spitting out a message every two seconds.


[2025-04-26 07:39:53.898] Bytes received: 56
[2025-04-26 07:39:53.898] Char: 1
[2025-04-26 07:39:53.898] Int: 26
[2025-04-26 07:39:53.903] Float: 1.20
[2025-04-26 07:39:53.903] String: Hello
[2025-04-26 07:39:53.904] Bool: 0
[2025-04-26 07:39:53.904]
[2025-04-26 07:39:55.901] Bytes received: 56
[2025-04-26 07:39:55.901] Char: 1
[2025-04-26 07:39:55.901] Int: 27
[2025-04-26 07:39:55.904] Float: 1.20
[2025-04-26 07:39:55.904] String: Hello
[2025-04-26 07:39:55.906] Bool: 0
[2025-04-26 07:39:55.906]
[2025-04-26 07:39:57.767] after delay
[2025-04-26 07:39:57.767] before delay
[2025-04-26 07:39:57.900] Bytes received: 56
[2025-04-26 07:39:57.900] Char: 1
[2025-04-26 07:39:57.900] Int: 28
[2025-04-26 07:39:57.904] Float: 1.20
[2025-04-26 07:39:57.904] String: Hello
[2025-04-26 07:39:57.907] Bool: 0
[2025-04-26 07:39:57.907]
[2025-04-26 07:39:59.901] Bytes received: 56
[2025-04-26 07:39:59.901] Char: 1
[2025-04-26 07:39:59.901] Int: 29
[2025-04-26 07:39:59.905] Float: 1.20
[2025-04-26 07:39:59.905] String: Hello
[2025-04-26 07:39:59.908] Bool: 0
[2025-04-26 07:39:59.908]

So, what is going on here and what is actually is blocked by ‘delay’.

]]>
By: Ton https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-834336 Thu, 11 May 2023 14:50:33 +0000 http://randomnerdtutorials.com/?p=15742#comment-834336 Hoi.
Interessant en leerzaam.
Ik ben zelf begonnen met programmeren in c en cpp.
Heel leuk en kan mooie dingen maken.

]]>
By: Dario https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-763809 Mon, 20 Jun 2022 22:32:29 +0000 http://randomnerdtutorials.com/?p=15742#comment-763809 Hi all. Here is an alternative way to insert into sketches instructions to blink a led : I put in the main loop the following
digitalWrite(BLINK_LED_PIN, ((millis() / 500) % 2 == 0)); // 0.5 seconds OFF and 0.5 Seconds ON

You can play with the 500 and 2 value to alter the type of blink. For example the following shows a brief light inpulse every second
digitalWrite(BLINK_LED_PIN, ((millis() / 50) % 20 == 0));

]]>
By: Sara Santos https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-763726 Mon, 20 Jun 2022 17:28:13 +0000 http://randomnerdtutorials.com/?p=15742#comment-763726 In reply to Muhammad Umer.

You want me to write code for your project for free???
Right…

]]>
By: Muhammad Umer https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-763367 Sun, 19 Jun 2022 14:46:34 +0000 http://randomnerdtutorials.com/?p=15742#comment-763367 ]]> In reply to Sara Santos.

That’s Okay! And Thanks for your kind replay
I have came out this problem by eliminating all delay() functions from whole code and replacing it with millis() function mechanism but still I’m surprised to face a generic issue while using mills(), Let me simplify it with a basic example of blinking LED..
“LED is connected with a digital pin of arduino .Also I have an input switch to arduino let say pinMode(8,INPUT); After some time when I start the arduino, if this input switch triggered (make it high or low depending on definition of condition) the LED will start blinking. “The main pint is that first led will be “”ON”” for 2000ms and then it would be off for 3000ms, Led by default would be low i-e int led= LOW; in definition stage before void setup(), In sample words the on-off sequence will follow rectangular wave staring with HIGH pulse at origin”. This sequence will be repeat again and aging while monitoring the status of input switch..
Any help
Need Code 😊

]]>
By: Sara Santos https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-763360 Sun, 19 Jun 2022 13:29:01 +0000 http://randomnerdtutorials.com/?p=15742#comment-763360 In reply to Muhammad Umer.

Hi.
Use flag variables inside the ISR instead of your whole function.
Then, check the state of those variables in the loop() and execute whatever tasks you need in the loop.
Regards,
Sara

]]>
By: Muhammad Umer https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-763111 Sat, 18 Jun 2022 13:30:30 +0000 http://randomnerdtutorials.com/?p=15742#comment-763111 Sir, I’m working on my Final Year Project “Portable Ventilator” where I have a lengthy source code which normally going in loop. Im looking for an interrupt function (hardware interrupt) that would be called when patient trying to breath on their own
(controlled from a pressure sensor) and ventilator will provoid breath on the spot. Unfortunately, Our ISR function contain a lot of delays (up to 500 milliseconds) but in ISR we can’t handle enough delays so what I can do now?
Plz any help

]]>
By: Dario https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-700181 Fri, 19 Nov 2021 10:29:51 +0000 http://randomnerdtutorials.com/?p=15742#comment-700181 Hi. When you compare intervals there is the problem of what happens when millis() rolls over when it reaches the uLong limit which happens about every 50 days. I have found many articles on how to properly code to avoid problems when the rollover happens. However I would like to test my code and I don’t want to wait for 50 days to validate my sketches behaviour 😉 . Is there a way to force the millis counter and set it just a few minutes before the limit (4294947295) ? I have found an article here https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover that explains how to do that on Arduino, but I have an ESP32 and would like something similar for ESP32. Any suggestion ?

]]>
By: Daniel Lybaert https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-576998 Tue, 23 Mar 2021 14:31:50 +0000 http://randomnerdtutorials.com/?p=15742#comment-576998 In reply to Sara Santos.

Pitty, thank You babe !

]]>
By: Sara Santos https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/#comment-576990 Tue, 23 Mar 2021 14:06:31 +0000 http://randomnerdtutorials.com/?p=15742#comment-576990 In reply to Daniel Lybaert.

Hi again Daniel.
I’m sorry, but we don’t do custom projects for our readers.
Regards,
Sara

]]>