Pixel manipluation interferes with millis() method.
I'm trying to set the color of a NeoPixel for a specified period of time, and then turn it off, using the millis() method to calculate the elapsed time. This code WORKS:
unsigned long TimePrior;
unsigned long TimeCurrent;
unsigned long Elapsed TIme;
// Turn the pixel on to WHITE
MyPixels.setPixelColor(0, COLOR_WHITE);
MyPixels.show();
TimePrior = millis();
TimeCurrent = millis();
TimeElapsed = TimeCurrent - TimePrior;
// Leave the pixel as WHITE until 10 seconds has passed
while (TimeElapsed < 10000) // 10,000 milliseconds
{
// DO NOTHING TO THE PIXELS
TimeCurrent = millis();
TimeElapsed = TimeCurrent - TimePrior;
}
// Turn the pixel off, i.e. to BLACK
MyPixels.setPixelColor(0, COLOR_BLACK);
MyPixels.show();
That is, if I set the pixel to WHITE and then just go into a loop and wait for the ten seconds to elapse and then set the pixel to BLACK after exiting the loop, it works just fine.
HOWEVER, if I do a MyPixels.show() WITHIN THE WAIT LOOP, it doesn't work properly. In fact, it waits for almost THIRTY SECONDS, instead of the expected ten seconds. That is, if I do THIS:
unsigned long TimePrior;
unsigned long TimeCurrent;
unsigned long Elapsed TIme;
// Turn the pixel on to WHITE
MyPixels.setPixelColor(0, COLOR_WHITE);
MyPixels.show();
TimePrior = millis();
TimeCurrent = millis();
TimeElapsed = TimeCurrent - TimePrior;
// Leave the pixel as WHITE until 10 seconds has passed
while (TimeElapsed < 10000) // 10,000 milliseconds
{
// INSTEAD OF DOING NOTHING TO THE NEOPIXELS, I call the show() method for the pixels.
MyPixels.show(); // << This is the ONLY DIFFERENCE from the above code.
TimeCurrent = millis();
TimeElapsed = TimeCurrent - TimePrior;
}
// Turn the pixel off, i.e. to BLACK
MyPixels.setPixelColor(0, COLOR_BLACK);
MyPixels.show();
Now, I know that I don't have to call the MyPixels.show() method from within the wait loop. However, this is just a simplification of what my program needs to do. My program actually does need to do some extra manipulation of the pixels while it's waiting for the ten seconds to pass.
However, when I do indeed call the show() method for the pixels within the wait loop it seems to screw up the millis() method, in that it (the millis() method) apparently is not returning the actual current time. It seems that the call to show() somehow causes millis() to NOT UPDATE properly and NOT TRACK the actual current time.
Any suggestions?