arduinoledneopixel

I have a LED Light strip and am not sure why the light isnt turning off immediately. Need code clarification


I'm fairly new at writing Arduino code and am confused by a block of code.

When executed, a blue dot travels down the LED strip and eventually loops back to the start.

#include <FastLED.h>
#define NUM_LEDS 150
#define DATA_PIN 4

CRGB leds[NUM_LEDS];

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

void loop() {
  for(int dot = 0; dot < NUM_LEDS; dot++) { 
    leds[dot] = CRGB::Blue;
    FastLED.show();
    // clear this led for the next time around the loop
    leds[dot] = CRGB::Black;
    delay(30);
  }
}

Following the void loop logic...


We set the first LED in the array to blue...
We then tell the strip to turn on with the .show()...
We then set the LED to Black... (This is where I'm confused)
And then set a Delay on the function...
Function then iterates the dot variable and continues down the LED strip.

Points of confusion

  1. When we set the LED to black, we never run another .show() method. How does the strip know to go to black?
  2. When I set the delay to something crazy like 300000, the blue light appears... waits the delay time.. then turns off and then iterates to the next LED. The behavior I would of expected would of been the light to turn blue... then immediately to black... and then wait for the delay timer.. then iterate to the next LED... I'm not sure how the delay is associated with the leds[dot] = CRGB::Black; line.

Solution

  • The first loop will have leds[0] turn blue, then it will display it on the light strip with FastLED.show(). Next it will make the same leds[0] turn black, however it will only display that in the second iteration of the loop.

    The second loop will do the same to leds[1], and so on.