I was testing with 2 LEDS and found the flashing of the first LED. I moved the cli / sei... Example..
// Where the WS2811 is attached (pin B4)
#define LED_BIT (1<<4)
#define LED_DDR DDRB
#define LED_PORT PORTB
#define LED_PIN PINB
#define NOP asm("nop\n\t")
int maxColorArr = 256;
int maxColorValue = 255;
class WS2811 {
public:
static void init() {
LED_PORT &= ~LED_BIT;
LED_DDR |= LED_BIT;
}
};
WS2811 ws2811;
// A single LED in the string
class LED {
byte r_,g_,b_;
static void sendByte(byte b) {
byte mask = 0x80;
while (mask!=0) {
if ((b&mask)==0) {
// Send a '0'
LED_PIN = LED_BIT; // Hi (start)
NOP; // Hi
LED_PIN = LED_BIT; // Lo (250ns)
NOP; // Lo
NOP; // Lo (500ns)
NOP; // Lo (data bit here!)
NOP; // Lo (750ns)
NOP; // Lo (875ns)
}
else {
// Send a '1'
LED_PIN = LED_BIT; // Hi (start)
NOP; // Hi
NOP; // Hi (250ns)
NOP; // Hi
NOP; // Hi (500ns)
NOP; // Hi (data bit here!)
NOP; // Hi (750ns)
LED_PIN = LED_BIT; // Lo (875ns)
}
mask >>= 1; // Lo (1000ns)
}
}
public:
// Set my color
LED& setColor(byte r, byte g, byte b) {
r_ = r;
g_ = g;
b_ = b;
}
// Send me to the LED
void send() const {
sendByte(g_);
sendByte(b_);
sendByte(r_);
}
};
LED led1;
LED led2;
void setup()
{
ws2811.init();
}
void getRGBFromIndex(byte colorIndex,byte *ValueR,byte *ValueG,byte *ValueB)
{
if (colorIndex < 43)
*ValueR = 255;
else
if (colorIndex < 86)
*ValueR = 255-(colorIndex * 6);
else
if (colorIndex < 171)
*ValueR = 0;
else
if (colorIndex < 214)
*ValueR = colorIndex * 6;
else
*ValueR = 255;
if (colorIndex < 43)
*ValueG = colorIndex * 6;
else
if (colorIndex < 129)
*ValueG = 255;
else
if (colorIndex < 171)
*ValueG = 255-(colorIndex * 6);
else
*ValueG = 0;
if (colorIndex < 86)
*ValueB = 0;
else
if (colorIndex < 128)
*ValueB = colorIndex * 6;
else
if (colorIndex < 214)
*ValueB = 255;
else
*ValueB = 255-(colorIndex * 6);
}
byte AutoColorIndex1 = 0;
byte AutoColorIndex2 = 128;
// The current RGB color values for mode 3 fade
static byte ValueR1;
static byte ValueG1;
static byte ValueB1;
static byte ValueR2;
static byte ValueG2;
static byte ValueB2;
void loop()
{
getRGBFromIndex(AutoColorIndex1,&ValueR1,&ValueG1,&ValueB1);
AutoColorIndex1++;
if (AutoColorIndex1 == maxColorArr)
AutoColorIndex1 = 0;
getRGBFromIndex(AutoColorIndex2,&ValueR2,&ValueG2,&ValueB2);
AutoColorIndex2++;
if (AutoColorIndex2 == maxColorArr)
AutoColorIndex2 = 0;
led1.setColor(ValueR1,ValueG1,ValueB1);
led2.setColor(ValueR2,ValueG2,ValueB2);
LED_PIN = 0; // Hi (start)
cli(); // Interrupts have to be off while we do this as they cause timing glitches
led1.send();
led2.send();
sei();
delay(100);
/*
byte b = 128;
led.setColor(b,0,b);
led.send();
led.setColor(0,b,0);
led.send();
delay(500);
led.setColor(0,b,0);
led.send();
led.setColor(b,0,b);
led.send();
delay(500);
*/
}