1

Im a complete beginner. Generally speaking, is there any need at all to have a delay in the main loop()? Im not used to writing infinite loops 😀

Is it correct to say that the delay() command is simply helpful for tutorials (i.e turn light on, wait, turn light off. wow!) but normally speaking a delay would not be helpful, and instead we are just rapidly infinitely polling our connected devices. Is that correct?

(I get that in some specialized cases it might be helpful to put a delay in the loop. Im not asking about the specialized cases.)

2
  • Like anything, it depends--I'm not sure there's a "normally speaking" given the wide range of Arduino applications. "delay" is perfectly suitable when there are no other time-sensitive processes (interrupts, background tasks, etc.) happening during the delay. Nov 7, 2021 at 16:13
  • @DaveNewton If you want to post an answer please do so. Comments under questions are for clarifying the question, which this comment does not do. If you make a proper answer you may well get credit for it, and people can comment on the accuracy or otherwise of your answer.
    – Nick Gammon
    Nov 8, 2021 at 6:24

2 Answers 2

3

There is no absolute need for any Arduino program to use delay()

The blink without delay sketch that is included in the standard sketch examples is the canonical illustration of this.

In most cases you don't need to poll your sensors thousands of times a second. It is good discipline to only poll sensors as often as makes sense for the specific application. Sometimes this might be once every five minutes. For example an ambient temperature sensor. In those cases there is nothing to be gained be polling a thousand times a second. To do so wastes resources and is a poor habit that could be troublesome for battery powered applications.

For example, the use of millis() for timing allows you to effectively poll several peripherals each at different intervals, perhaps averaging some readings to reduce noise, perhaps also updating a display at other intervals that suit human perception.

For long running battery powered applications it is likely that you will benefit from finding out about putting the microcontroller to sleep using sleep() or deepSleep() and using interrupts to wake it. A real time clock (RTC) or the watchdog timer interrupt can perhaps be used to schedule your sensor readings and other actions, perhaps in conjunction with millis().

For very simple programs that don't need precise timings. delay() can be the simplest and clearest way to manage timings.

2
  • +1 Good answer. I would add that sometimes one needs to rate limit specific actions, which rely on hardware or other code, to avoid problems (like buffer overruns, filling up the Serial buffer, which then blocks execution),...). Using delay() is one easy way to do it, though by far not the best.
    – chrisl
    Nov 7, 2021 at 12:39
  • In many cases, rate-limiting the polling is only useful if you sleep. On a mains-powered application, polling thousands of times a second is fine, and is easier. Nov 7, 2021 at 12:55
1

No, there is no need to use delay() at all. Unlike the sleep() or deepSleep() commands, delay does not save power or do anything else that's special.

The standard implementation of delay is just doing a busy-wait until the time has elapsed, something like:

void delay(int ms)
{
    int end = millis() + ms;
    while (millis() < end) {}; // do nothing
}

So for the CPU itself, this just wastes CPU cycles, it doesn't save any power.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.