Page 1 of 1

Dimming Neopixels, Delays<Microseconds

Posted: Wed May 01, 2019 4:32 pm
by HelWeb
Neopixels have great possibilties. With a normal LEDs you can show the state of a system (blue= too cold, red= too hot). With a neopixel you can show values in between with smoothly changing colors from for instance blue ... many colors ... red.

Dimming an LED may be done with PWM - not so with a Neopixel.
And Neopixels have crude timings. (400 ns)

Here is a very small RTOS-task dimming a Neopixel with different colors.
BTW It can be used for very short delays below 1 microsecond.

Have fun ;)

Code: Untitled.c Select all


// ----------- NeoPixel -------------
//---------- WS2812B.pdf ------------

inline void BitL(int lp) {
int ii;
gpio_set_level( RTOS_PinNeo, 0);
for (ii=0; ii<lp; ii++) {
asm(" nop");
}

}

inline void BitH(int lp) {
int ii;
gpio_set_level( RTOS_PinNeo, 1);
for (ii=0; ii<lp; ii++) {
asm(" nop");
}
}

inline void Bit1() {
BitH(21);
BitL(6); // 21/6 // from experiments
}

inline void Bit0() {
BitH(8);
BitL(20); // 8/20
}


inline void Color( uint32_t c) {
int i;
for (i=0;i<24;i++) {
if (c & 0x800000 ) Bit1();
else Bit0();
c<<=1;
}
}

/**
* \name NeoPix
* @{
* Dimming Blink of NeoPixel, 3 colors: red, green, blue
*
* \hideinitializer
*
*/

void NeoPix( void * pvParameters ) {
uint32_t c=0, cc=0;
int up=1;
unsigned int state=0;
gpio_set_direction(RTOS_PinNeo, GPIO_MODE_OUTPUT ); // Data-Pin of NeoPixel
c=0x01;
while(1) {
portDISABLE_INTERRUPTS();
Color(cc);
BitL(2);

//SentColors++; // for testing
// Dimming and blinking ...
if (up) { c+=1; if (c>=0xfe) up=0;}
else { c-=1; if (c==0x000000) { up=1; state+=8;} } // BRG: shift 0, 8, 16
if (state>16) state=0;
cc = (c<<state);
portENABLE_INTERRUPTS();
vTaskDelay(1);
}

}

// --- End NeoPixel
/** @} */

Re: Dimming Neopixels, Delays<Microseconds

Posted: Thu May 02, 2019 2:37 am
by Sprite
FWIW, while this may be an interesting example, the ESP32 has a bunch of peripherals that are better at doing this than dedicating a core to it... the RMT, I2S and SPI peripherals all have been used to do this with minimal CPU overhead; various libraries are available for this.

Re: Dimming Neopixels, Delays<Microseconds

Posted: Thu Jun 06, 2019 1:53 pm
by fasani
Very interesting thanks for your code example.
I've also started a project using Neopixels to receive short UDP messages and launch fast animations
https://github.com/martinberlin/Remora

I would like to send signals from Orca sequencer

Re: Dimming Neopixels, Delays<Microseconds

Posted: Mon Oct 07, 2019 3:55 pm
by fasani
Totally agree with ESP_Sprite.

Note that if you are using Neopixels Makuna library there is a I2S bug that is sometimes not refreshing the leds correctly. Use the RMT methods for the ESP32. Then you can have as much as 8 channels and control a lot of pixels.