From the arduino reference page for delay the parameter for delay is an unsigned long
Unsigned longs on the arduino can reach from 0 to 4,294,967,295.
It is likely that the number being passed to 'delay' is being interpreted as an int. This would mean the delay is limited to a max of 32,767.
You should explicitly declare your delay value as an unsigned long like the solution in this post.
unsigned long seconds = 1000L; //Notice the L
unsigned long minutes = seconds * 60;
delay(minutes); //for 60,000 milliseconds
Answer from Wil on Stack OverflowFrom the arduino reference page for delay the parameter for delay is an unsigned long
Unsigned longs on the arduino can reach from 0 to 4,294,967,295.
It is likely that the number being passed to 'delay' is being interpreted as an int. This would mean the delay is limited to a max of 32,767.
You should explicitly declare your delay value as an unsigned long like the solution in this post.
unsigned long seconds = 1000L; //Notice the L
unsigned long minutes = seconds * 60;
delay(minutes); //for 60,000 milliseconds
Yeesh... someone cited this thread elsewhere, here is the correct explanation:
delay() takes an unsigned long (32-bit number between 0 and 2^32-1, or about 4.2 billion). And delay(60000) works fine. There are two ways that one could get into trouble trying to pass numbers larger than those used in blink, but smaller than 4.2 billion to delay()
First, if you generate the number on the spot by multiplying numbers together, for example delay(60*1000);
Both of these numbers will default to int (16-bit signed integer from -32768 to 32767), so that multiplication will overflow that… leaving you with -5536 if my math serves me… Then, that gets converted to an unsigned long, leaving you with 2^32-5536, or, well, approximately 4.2 billion.
Solution is simple - explicitly tell the compiler that you intend for that literal to be an unsigned long, by putting UL after one of the numbers (the other will be automatically promoted to unsigned long to match):
delay(60UL*1000);
The other way an arduino user could get in trouble here (really, the same way, only through a different route) would be if they assigned the delay to a data type that couldn’t fit it, for example:
int time=60000; //this ends up as -5536 too...
…
delay(time); //same result!
Solution is also quite simple - just declare time as an unsigned long, instead of int.
Note that the first case (but not the second), where the constant expression generates an overflow, would actually generate a compiler warning…. Except, arduino turns off compiler warnings by default. You can turn them back on in File -> preferences. Sometimes it will warn about things that are fine, but when you see warnings, and it’s not doing what you want, the warnings are a good place to start looking.
Background on delayMicroseconds()
The issue with delayMicroseconds() having the maximum length of 16383 is unrelated - that limitation is because delayMicroseconds is, under the hood, implemented completely differently from delay(). delay() is based on micros() and the millis timer system (a hardware timer is configured prior to setup() being called, with the overflow interrupt incrementing a global count of milliseconds; millis() uses that count, and micros uses that plus the current count on the timer). delayMicroseconds, however, needs much higher accuracy than that method could achieve (micros takes something like 7-8us to return, and has resolution of 4us, since the timer in question runs off the 16MHz clock with /64 prescale); it's implemented by counting clock cycles. It takes an unsigned int (16-bit) instead of unsigned long, first off. It multiplies the requested delay by 4 (for 16MHz clock, at least), converting it into the number of iterations of a loop written in inline assembler that takes 4 clock cycles per iteration. So that's where the apparent 14-bit limit comes from: the number of 4-cycle loops has to fit in 16 bits, so the number of microseconds is 1/4th that (assuming 16 MHz system clock - so an 8 MHz pro mini, for example, you could specify twice as long of a delay to delayMicroseconds()!). On an (admittedly rare) 20 MHz one, the longest delay would be around 13000 ((2^16-1)/(20/4=5)) Long before that, though, you're better off just doing a while loop that checks micros()...
Max Delay - Direct Answer requested
delay doesn't accept variable for long times?
Time limit on delay()?
maximum delay() is 32767ms ?
I am having some trouble with my Leonardo. It seems to be getting stuck in things that should not be infinite loops. What's shown below is what I originally wrote, but my Void loop function does not loop. It only runs once. The intent is to refresh the page every approximately 2 minutes, then click on three buttons that are 25 units apart. (I also made the mouse move in the x direction just because after a refresh, the mouse icon was hidden so I wasn't sure whether to trust that it would click the right spot until I made it visible again).
#include <Keyboard.h>
#include <Mouse.h>
const char refreshKey = KEY_F5;
const int ySpacing = 25;
const int xSpacing = 500;
const int refreshDelay = 10000;
const int eventDelay = 50;
const int pauseDelay = 150;
const int interluDelay = 100000;
void setup() {
Keyboard.begin();
delay(refreshDelay / 5);
Mouse.begin();
delay(refreshDelay / 5);
}
void loop() {
// refresh page
Keyboard.press``(refreshKey);
delay(eventDelay);
Keyboard.releaseAll();
delay(refreshDelay);
// click first
Mouse.move(xSpacing, 0, 0);
delay(pauseDelay);
Mouse.press``();
delay(eventDelay);
Mouse.release();
delay(pauseDelay);
// click up one
Mouse.move(0 - xSpacing, 0 - ySpacing, 0);
delay(pauseDelay);
Mouse.press``();
delay(eventDelay);
Mouse.release();
delay(pauseDelay);
// click up two
Mouse.move(0, 0 - ySpacing, 0);
delay(pauseDelay);
Mouse.press``();
delay(eventDelay);
Mouse.release();
delay(pauseDelay);
// return mouse and wait
Mouse.move(0, 2 * ySpacing, 0);
delay(interluDelay);
}
Like I said, it runs once, it works perfectly, but then it stops for some reason. One thing I did to check to see if it was truly looping or not was to include a piezo buzzer that beeps for about a half-second at the beginning of Void loop. It still wasn't repeating, so I took out everything except the beep and the long, 100 second delay. That code looks like this:
void loop() {
for (int j = 0; j < 50; j++) {
digitalWrite(buzzerPin, HIGH);
delay(3);
digitalWrite(buzzerPin, LOW);
delay(3);
}
delay(interluDelay);
}
Anyway, I tried a lot of things from deleting all references to the libraries, shortening variable names, getting rid of all constants that were no longer being used due to commenting out 3/4 of my code. Finally, I just got rid of the variable interluDelay alltogether and wrote 100,000 in the delay function manually, and it worked. Arduino's max int value is 2 billion, and the maximum delay time is twice that. Why can I type the number in but can't use a variable (and why were all my other variable delays like the 10 second refreshDelay were working just fine?)
Hello!
Noob to Arduino and C here.
I'm working with a stepper motor to control the Right Ascension axis on my telescope.
I've found that between two steps, I need a delay of 25.882 milliseconds. Now, delay() only takes integers, but I need a higher resolution. Looking at delayMicroseconds(), it states that for this delay, a maximum of 16383 microseconds is possible for accurate timing. Obviously, I need 25882 microseconds, which is above that limit.
What I'm wondering, is: Can I do two consecutive delayMicroseconds() with half my target delay? This means that I'll do
delayMicroseconds(target_half) // target_half = 12941 < 16383 delayMicroseconds(target_half)
Here's a code exerpt:
float millisbetweenSteps = 25.882; // milliseconds - calculated - lower is faster
int microsbetweenSteps = millisbetweenSteps * 1000; // microseconds
...
void runStepper(boolean run) {
if (run == true) {
digitalWrite(enablePin, HIGH);
digitalWrite(directionPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros); // needed?
digitalWrite(stepPin, LOW);
delayMicroseconds(microsbetweenSteps/2); //Because delayMicroseconds max value is 16383micros
digitalWrite(ledPin, !digitalRead(ledPin));
delayMicroseconds(microsbetweenSteps/2);
}
}
void loop() {
runStepper(true);
}Looking at the stepper, it looks as if I'm getting the desired result, but I'm not sure.
Cheers!