# WS2811 and FastSPI

Hi,

I have some ws2811 strips, 32 led long, 5v, and have it correctly setup and running the test examples as set out from the fastspi library.

I can see the pretty patterns, and can change the patterns, but I want to understand how to address a single led with a colour.

I don’t understand the maths.

``````void police_lightsONE(int idelay) { //-POLICE LIGHTS (TWO COLOR SINGLE LED)
idex++;
if (idex >= NUM_LEDS) {idex = 0;}
int idexR = idex;
int idexB = antipodal_index(idexR);
for(int i = 0; i < NUM_LEDS; i++ ) {
if (i == idexR) {set_color_led(i, 255, 0, 0);}
else if (i == idexB) {set_color_led(i, 0, 0, 255);}
else {set_color_led(i, 0, 0, 0);}
}
FastSPI_LED.show();
delay(idelay);
}
``````

is a portion of the code, which is probably needless, but it’s what i’m staring at!

That piece of code doesn’t help because it’s calling additional functions that you didn’t provide. However, FastSPI is a very simple library to work with. It first creates an array ‘leds’ that you can use to address the entire string. If your string has 20 pixels on it, you would’ve set NUM_LEDS to 20, which in turn sets that ‘leds’ array to the correct amount of indexes.

You can loop through the whole array to set a color, for example:

``````memset(leds, 0, NUM_LEDS * 3);  // this resets all values to zero
for (int px = 0; px <= NUM_LEDS; px++) {
leds[px].r = 255;
leds[px].b = 0;
leds[px].g = 0;
}
FastSPI_LED.show();
``````

That simply sets each pixel to the color red, then “shows” the result.

If you want to address a specific pixel, do that by passing it’s index. For example, if you want to address the 10th pixel:

``````memset(leds, 0, NUM_LEDS * 3);
leds[9].r = 255;
leds[9].g = 0;
leds[9].b = 0;
FastSPI_LED.show();
``````

Now only the 10th LED should light up red. Remember arrays start at 0, so the 10th LED is #9 in the array.

perfect! that's exactly the info i was looking for!

thanks