NeoPixel manipulation interferes with millis() method

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?

Any suggestions?

Print the value returned by millis() before and after the call to show().

Look at the source code for the library. See if it uses the same timer that millis() uses.

RogerInHawaii:
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.

Yes, of course.

With WS2811/WS2812 LEDs the show() method blocks all interrupts in the system, including Timer0 interrupts, which are providing the Arduino timing.

So if 'show()' lasts around 1 millisecond (very few LEDs), everything is fine.
Otherwise you will lose Timer0 interrupts and millis() counting.

How many LEDs do you control with 'show()'?

RogerInHawaii:
Any suggestions?

What about WS2801 LEDs which are controlled by SPI and have a seperate clock line? Control of WS2801 LEDs works without blocking interrupts and therefore doesn't affect the Arduino timing.

Thank you so much for the very helpful information. I guess I'll have to take a different approach to deciding when to update the neopixels. My project is way too far along to switch to different neopixels. It is only at this point in the development that the timing of the pixels becomes critical, so it was frustrating to find out that the neopixel show() method interferes with the millis() function.