Should I use delay() in loop, if not particularly needed?

Hello, I’m a new guy in Arduino stuff, I’ve some experience in 3D game programming.

I just wrote a code for my servo that checks whether the button is pressed. It doesn’t need any delay() functions in the loop, but it also doesn’t need to go through lightning fast. Once every 100ms e.g. would be fine.

What do I achieve by using delay() in terms of power consumption, efficiency etc.? Should I stick delay(100) just for that sake, or does it hurt my program somehow?

Pretty much summed up: Is it better to delay main loop as much as possible, or as less as possible?

This is pretty much one-time-only question that I need to get out of the way so I don’t go with the wrong choice. Just for the interest for the core mechanics and optimization. Thanks!

What do I achieve by using delay() in terms of power consumption, efficiency etc.?

Nothing, nothing, and nothing.

Should I stick delay(100) just for that sake, or does it hurt my program somehow?

No, it is not needed. It won't hurt anything, except that it will make your switch less responsive.

Is it better to delay main loop as much as possible, or as less as possible?

As little as possible.

But if I need to use timers, I found out that I have to use larger numbers because the program runs faster.

For example, I need to time that user has held the button for 3 seconds. I do this by increasing TIMER value(++timer;) when button is pressed. If I don't have any delays restricting the main loop, then I have to wait it to reach numbers like 10 000, instead of like 300. This takes away memory, doesn't it?

Or should I use different type of timer instead of increasing variable?(Like millis()?) That would pretty much solve this problem, so I could run the loop at any rate with fixed timings.
But I'd need to run the device more than 50 days(hopefully, it's battery powered), so would the internal timer overflow?

Thanks for very fast and good response!

For example, I need to time that user has held the button for 3 seconds. I do this by increasing TIMER value(++timer;) when button is pressed.

That is the wrong approach. Note the time when the switch is pressed (using millis() or micros(), depending on the level of accuracy you need) and note the time when the switch is released. The difference is the time that the switch was held down.

If I don't have any delays restricting the main loop, then I have to wait it to reach numbers like 10 000, instead of like 300. This takes away memory, doesn't it?

300 and 10000 are both ints. No more memory is needed, though, the approach is still wrong.

As you develop more complex code, the number of iterations of a function becomes a poorer and poorer measure of elapsed time.

Or should I use different type of timer instead of increasing variable?(Like millis()?) That would pretty much solve this problem, so I could run the loop at any rate with fixed timings.

Yes, you should use millis(). You can't "run the loop at any rate with fixed timings". The function of the program, external and internal interrupts, and user input will dictate the timing, which can vary from one iteration to the next.

But I'd need to run the device more than 50 days(hopefully, it's battery powered), so would the internal timer overflow?

Your watch overflows every day. When was the last time that caused you a problem?

PaulS:

But I'd need to run the device more than 50 days(hopefully, it's battery powered), so would the internal timer overflow?

Your watch overflows every day. When was the last time that caused you a problem?

To be more informative: overflow of micros() or millis() result is not a problem so long as you always deal in time-differences, and that these time differences are less than the wrap-around time. You should always use this kind of idiom:

  if (millis() - prev_time >= DELAY)

The difference between two time values will still be valid across a wrap-around, so this will always work (assuming DELAY is representable as an long)

If you do things this way it will break at the wrap-around:

  if (millis() >= prev_time + DELAY)

[ Oh yes, the other thing, delay() is itself a loop, which is why it offers no efficiency benefit - you can go and look at the code of delay() if you want, this is open-source - often the best way to answer questions about the code ]

I got you! I was just worried about how the microcontrollers deal with overflows, does it freeze or something, but it only resets like a clock, excellent analogy.
And the timer value is irrelevant as long as dealing with differences, I'm already familiar with this from game programming.

Thank you guys! Arduino has indeed a great community.
I'm off to reprogram my timing with millis()!

PaulS:
Your watch overflows every day. When was the last time that caused you a problem?

My clock overflows every 12 hours. Damn annoying.

I'm off to reprogram my timing with millis()!

Blink without delay as always. Also, look at the "timing" section in the playground. Lots of libraries to make your life (potentially) easier in the long run...

:slight_smile:

My clock overflows every 12 hours.

Does it have a crystal or a resonator? Are you running it with the right voltage? Are you running it at the right temperature?

What evidence do you have that it actually overflowing that often? Where is your code?

Well, PaulS, let me explain ...

I believe I am running it at the right voltage. I put in a 1.5V AA battery. Duracell, I think, if that makes a difference. The temperature is around 20 (celsius) although it tends to rise a bit in the afternoons. I haven't noticed any crystal or resonator.

Now, at 11 in the morning I put on a roast turkey, which I need to cook for two hours, so naturally I expect it to finish at 13 hours. But the clock only goes up to 12! So it never reaches 13, and the turkey is burnt to a crisp. I'm at my wits' end. So I'm pretty sure it is overflowing (is that the right word?).

I hit upon the idea of resetting the clock. That is, when I put the turkey in, I crank the hands back to zero. Now I don't know if you own a clock, but that isn't that easy to do, as there is no zero on the dial. So what I do is go to "1", and move the hands back to just before "12" and hope for the best.

That works well enough, but I do find that when I am cooking I lose track of time. It always seems to be between 12 and 2.

And the worst part is, if I want to cook potatoes for one hour (while the turkey is cooking) I have to find a second clock!

I hope you can advise me on how to do this in a simpler way.

I hope you can advise me on how to do this in a simpler way.

Maybe when I finish laughing. Too funny.