Every nth LED turned on?

Every nth LED turned on?

If I understand correctly (I’m stuck at work and can’t run the sketch), the following would turn on – and leave on – every 5th LED, one at a time.

void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
for(int i = 0; i < NUM_LEDS; i+5)
{
leds[i] = CRGB::Red;
FastLED.show();
delay(1000);
}
}

Is there a way to get every 5th LED turned on at once? I’ve been playing with it for a couple of hours but can’t seem to math up anything that makes sense.

Just thought of something. The below verifies on Codebender, but (again, still at the office) can’t test.

Can I “isolate” the fastLED.show until all the math is done?

void loop()
{
for(int i = 0; i < NUM_LEDS; i+5)
{
leds[i] = CRGB::Red;
{
FastLED.show();
delay(1000);
}
}
}

Did you mean “i += 5” instead of “i+5” in the for loop? The expression “i+5” will only take the value of i and then add 5 but not do anything with the result.

The way it is it never shows anything so im sure you wanna put in i+=5 instead

Thanks for catching that! I’m still at the very basics of coding.

Calling FastLED.show(); will update your pixels with whatever changes have been made since the last time show() was called. You can update a single pixel and then call show() to see the change for that single pixel, or update 100 pixels and then call show() and have them all update simultaneously.

You can pull the show() out of the loop like below and then all changed pixels will update together.

void loop() {
for(int i = 0; i < NUM_LEDS; i+=5) {
leds[i] = CHSV(random8(), 255, 255); // choose random color
}
FastLED.show();
delay(1000);
}

You can update the value for a pixel a dozen times, but you won’t see those changes if you don’t call show() after each change. This can sometimes be used to your advantage. For example, the below first fills the entire strip with a random color, and then every fifth pixel with another color. By waiting to call show() until the end we only see the final result and not the intermediate step of filling the entire strip a single color.

void loop() {
// fill entire strip will random color
fill_solid(leds, NUM_LEDS, CHSV(random8(),255,255) );

uint8_t myColor = random8();  //pick a random color 

for(int i = 0; i < NUM_LEDS; i+=5) {
    leds[i] = CHSV(myColor, 255, 255);
}

FastLED.show();
delay(1000);

}

Wow! Thanks for the EXTRA helpful info. Much appreciated!