Neopixels (WS2812 or SK6812), interrupts and IntervalTimer

Status
Not open for further replies.

tenkai

Well-known member
Just thought I would share a fun little hack I discovered.

Neopixels (WS2812b and SK6812) LEDs use a single data pin that require a fixed update frequency. Because of this, the update routines in the libraries involved require that noInterrupt() is called before writing data to the LEDs, and then interrupts() is called again after.

I have a number of interrupts I need to run that are using IntervalTimer. There is only one that would cause timing errors if it was paused in such a manner to update LEDs. This interrupt was running a very simple routine and would execute and return extremely quickly, no heavy lifting was done in it at all, just incrementing a few variables to keep timing.

I commented the noInterrupt() and interrupt() calls from the Adafruit Neopixel library I am using, and put the all the LED update code into an IntervalTimer interrupt routine itself, and using IntervalTimer::setPriority(uint8_t priority) I set the priorities for each IntervalTimer. The highest priority (lowest number) IntervalTimer is the timing routine, and the LED update IntervalTimer is the second highest priority. The other IntervalTimer I am using does a lot of work (uses SPI etc), and runs at a lower frequency, so that is the lowest priority.

Even though the LED update routine is being interrupted, the code that needs to run during that interrupt returns so quickly that it does not interfere with LED updates.

An alternative to this would be using Paul's OctoWS2811, which can handle interrupts as well, but I did not have the correct pins available to implement this.
 
Status
Not open for further replies.
Back
Top