Max Delay - Direct Answer requested

I happened to see someone ask about the maximum delay time possible in an Arduino sketch. Simple question. But I didn't see that anyone actually answered the question. There were about 19 "why do you want to do it that way"'s, and about 27 "you should use millis()"'s, and several "let's take a look at your code"'s. But I couldn't see where anyone actually answered the question.

In looking further into it, it seems that people have been asking for years, and if it got answered, I didn't see it.

Let's stipulate that it is a bad way to program, that it ties up the processor, and so on. But if some maniac wanted to know, just to know, is there a maximum number of millisecond delay built into the Arduino Uno ? What would the answer be ?

1 Like

Infinite.

Thanks.

If you are asking ("requesting") about the function delay(unsigned long), then the maximum value you can pass in is 2^32-1. Remember that these are milliseconds.

I am not sure how to respond. I don't remember seeing any sketch (but I haven't that many) where delay was assigned a data type. You just call delay().

I take it from your question that there is a default data type associated with delay, and that that type can be changed. Is that right ?

pratto:
I am not sure how to respond. I don't remember seeing any sketch (but I haven't that many) where delay was assigned a data type. You just call delay().

I take it from your question that there is a default data type associated with delay, and that that type can be changed. Is that right ?

That's because variables can be promoted to a larger data type if passed to a function that expects a compatible type. Read the documentation for the delay() function that is in the reference menu of the IDE for details. It explains there that the passed parameter is an unsigned long. It's because of the promotion that I don't have to pass an unsigned long specifically, as in:

delay(500UL);

Instead I can pass 500 and the int will be promoted to an unsigned long.

delay(500);

with the same effect.

If you look at the prototype for any function it should show the return type and the passed parameter types. delay() accepts an unsigned long for a passed parameter so the largest value it can take is 0xFFFFFFFF

void delay(unsigned long)

This means it returns nothing and expects an unsigned long as a passed parameter. Parameters may be promoted as explained in the previous reply.

I happened to see someone ask about the maximum delay time possible in an Arduino sketch. ... But I couldn't see where anyone actually answered the question.

That's because it is a philosophical question not a programming question. As a modern general purpose processor it is capable of any length delay up to the survival limit of our planet. We do not know the survival limit of our planet and will very likely never know so, from our perspective, there is no answer.

ok. about 49.7 days. Now how can I remember that number in case some maniac asks me.

thanks all.

void setup()
{
  pinMode(9,OUTPUT);
  
}
void loop()
{
  delay(0xFFFFFFFF + 1000);
  digitalWrite(9,HIGH);
  
}

Pin 9 goes high after 1 second, so max delay() is 4,294,967,295 millis unless another variable is used to count rollovers.

The question is poorly posed. He didn't ask about the delay() function, at least not initially. But even if with delay() everyone seems to be assuming a 16MHz clock. There are standard Arduinos with 8MHz clocks, not that he even specified that it be a standard Arduino. And you can slow the clock down in setup() to kilohertz frequency. You can also put it to sleep. Or you could loop and call delay. Or just loop with no possible termination. Forever.

Okay, forever is a long time. Even the lifespan of our planet is likely to be a lot longer than the lifespan of an AVR processor.

What year will the last AVR stop running?

jcallen:

void setup()

{
  pinMode(9,OUTPUT);
 
}
void loop()
{
  delay(0xFFFFFFFF + 1000);
  digitalWrite(9,HIGH);
 
}



Pin 9 goes high after 1 second, so max delay() is 4,294,967,295 millis unless another variable is used to count rollovers.

No. Pin 9 goes high after 999 milliseconds, 0xFFFFFFFF + 1 = 0.

jboyton:
What year will the last AVR stop running?

The last AVR will stop running less than a second after the last power supply fails.

@Henry_Best:

You know, I thought it seemed a bit early.


But even if with delay() everyone seems to be assuming a 16MHz clock. There are standard Arduinos with 8MHz clocks

The delay() function is implemented to measure milliseconds regardless of the frequency, no?

Yes, setting the bootloader clock speed in boards.txt should determine how often the mS counter kicks off a tick.

uno.build.f_cpu=16000000L

But unsigned long still holds the amount of mS ticks, so the 49.7 days is the same for 8MHz or 16MHz.

pratto:
Let's stipulate that it is a bad way to program, that it ties up the processor, and so on. But if some maniac wanted to know, just to know, is there a maximum number of millisecond delay built into the Arduino Uno ? What would the answer be ?

The millis function wraps around every 49.71 days, so that's the maximum delay (because all the delay function does is to poll millis()).

If you write your own delay function, you can improve in this, viz:

for(int i = 0; i<10000; i++) delay(30L*24L*60L*60L*1000L);

which will delay for 10,000 30-day periods. About a thousand years.

Is there a maximum? Sure! if you use 1k of memory for state, you can count to 2^(1024*8), about 10^2466 (1 followed by 2466 zeros). Multiply that by 30 days (the amount you can get out on one call to delay()), and that's how much of a delay you can get using 1k of memory for state.

But I think the sun is due to go red giant in a few billion years. A billion years is about 10^10 30-day periods, which is way less than 10^2466.

And that's why people are finding your question a little pointless. First, it's 50 days with delay(); and second, "what's the maximum delay?" is a question ill-defined enough to admit of answers like these.

The other very important reason is that delay() is not going to be accurate enough over those kinds of periods of time. You can delay for a week, if you want, but after a couple of months of operation the "OK guys! Start now!" time is going to be far enough off that it wont be the same time of day. Once you start doing thaty, you are better off with a Real Time Clock (RTC).

pratto:
ok. about 49.7 days. Now how can I remember that number in case some maniac asks me.

In case some maniac asks you about the maximum delay, hmm? Seems to me that happened to me recently. :stuck_out_tongue:

All you have to remember is that unsigned long is 32 bits, and then do some simple arithmetic:

2^32 / 1000 / 60 / 60 / 24 = 49.710 days
(2^32-1) / 1000 / 60 / 60 / 24

:smiling_imp:

I think you are right, since a delay of 0 is an acceptable value. :slight_smile:

I'll recalculate:

(2^32-1) / 1000 / 60 / 60 / 24 = 49.710 days