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.
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). 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(). 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. You can turn them back on in File -> preferences. Except, arduino turns off compiler warnings by default. Note that the first case (but not the second), where the constant expression generates an overflow, would actually generate a compiler warning…. Solution is also quite simple - just declare time as an unsigned long, instead of int. 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: 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): 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įirst, if you generate the number on the spot by multiplying numbers together, for example delay(60*1000) īoth 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. someone cited this thread elsewhere, here is the correct explanation:ĭelay() takes an unsigned long (32-bit number between 0 and 2^32-1, or about 4.2 billion).