-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
This is Issue 237 moved from a Google Code project.
Added by 2010-04-29T19:41:45.000Z by wes...@gmail.com.
Please review that bug for more context and additional comments, but update this bug.
Closed (Fixed).
Original labels: Type-Defect, Priority-Medium, Milestone-0019, Component-Core
Original description
delay() has always been pretty inaccurate for low values of milliseconds,
since it is based off of millis() which is update asynchronously to delay()
calls, and sometimes is doubly-incremented (when the overflows catch up.)
This was also changed relatively recently such that millis() is now
supposed to delay for "at least" the specified number of milliseconds, but
this doesn't work all the time (ie if called just before one of those
double-increments) and will "typically" cause delay() to wait significantly
longer than specified.
There is some pretty detailed analysis in this discussion:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271719397
BenF came up with a new proposed definition of delay() (attached) that
apparently works MUCH better, and is smaller as well.
void delay_x(uint32_t millis_delay)
{
uint16_t micros_now = (uint16_t)micros();
while (millis_delay > 0) {
if (((uint16_t)micros() - micros_now) >= 1000) {
millis_delay--;
micros_now += 1000;
}
}
}