Hello, Im relatively new to FastLED and I have some conceptual question about referencing

Hello,

Im relatively new to FastLED and I have some conceptual question about referencing nonconsecutive elements in a CRGB array. Specifically, i would like to do something like this:

leds1[AllEvens] = CRGB::Green;
leds1[AllOdds] = CRGB::Black;
FastLED.show();
delay(1000);
leds1[AllEvens] = CRGB::Black;
leds1[AllOdds] = CRGB::Green;
FastLED.show();
delay(1000)

I want all the even and odd lights to alternate on and off, giving sort of a marquee effect. I’m going to have a tree made of 6 strings all doing the same thing, so it will look like bands are moving up/down the tree as a whole. I also want to expand this for an arbitrary gap, say have the bands spaced by 2 or 3 pixels rather than 1 in my example above,

Now these strings are going to be 166 lights long, so it’s not really practical or efficient to assign all the elements to the desired color individually. it would be acceptable to create a list of all the even and odd indices beforehand and somehow assign all the colors together (as i’ve shown above, AllEvens and AllOdds being these lists)

I’ve been able to do some pretty complex stuff with FastLED, but I can’t for the figure out how to, in one line, assign a color to two or more pixels that are NOT consecutive. For example set ONLY pixels 0 and 2 to blue on the same line. If I can figure out a way to do that, I’m sure I can figure out the rest

Im using Windows with Arduino v1.8.4, FastLED 3.1.6 for a Mega 2560

One way would be to first fill the whole strip black and then use a loop that counts up by two to fill green, then repeat. Fill all black and loop again but start counting at led 1 instead of 0.

You can use a global Boolean variable that you switch back and forth to determine if you’re lighting up odds or evens each time around the main loop.

Another way would be to check if a pixel is even or odd as you go.
if ( n % 2 == 0) {
//is even number
} else {
//is odd number
}

And here’s a marquee example that might give you some other ideas.

More ideas for you.

Very helpful, thank you. I was concerned about being able to assign all the colors quick enough to make it appear simultaneous, but if I don’t show the changes until the loop is complete, that problem goes away. For posterity, my solution:

void loop() {
fill_solid(leds,NUM_LEDS,CRGB::Black);
for (int i=0; i<NUM_LEDS; i=i+2) {
leds[i] = CRGB::Green;
}
FastLED.show();
delay(1000);
fill_solid(leds,NUM_LEDS,CRGB::Black);
for (int i=1; i<NUM_LEDS; i=i+2) {
leds[i] = CRGB::Green;
}
FastLED.show();
delay(1000);
}