0

I am trying to blink a Grove - RGB LED Stick (10 - WS2813 Mini 3535) with this code:

#include "FastLED.h"

int ledState = LOW;       // ledState used to set the LED
long previousMillis = 0;  // will store last time LED was updated
long interval = 1000;     // interval at which to blink (milliseconds)

#define NUM_LEDS 10
CRGB myLeds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, 4>(myLeds, NUM_LEDS);
}

void loop() {
  unsigned long currentMillis = millis();
  FastLED.setBrightness(25);
  
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      for (int i = 0; i < NUM_LEDS; i++) {
        myLeds[i] = CRGB::White;
        FastLED.show();
      }
    } else {
      for (int i = 0; i < NUM_LEDS; i++) {
        myLeds[i] = CRGB::Black;
        FastLED.show();
      }
    }
  }
}

Any idea why it does not blink?

Thanks

5
  • 2
    Your code sets the LEDs color depending on the value of ledState, but its value is never updated after initialization, so your LEDs remain white.
    – StarCat
    Sep 14, 2023 at 10:08
  • Thanks. Added the ledState = HIGH; and ledState = LOW;
    – dkin
    Sep 14, 2023 at 11:16
  • @dkin did it now work with adding this? Sep 14, 2023 at 11:41
  • Yes. Any other idea/comment is always welcome
    – dkin
    Sep 14, 2023 at 12:18
  • Well, if the correction of a coding error, which was pointed out by @StarCat , solved the original problem then StarCat should convert the comment into an answer. Failing that, after waiting a reasonable period, the OP should write the answer.
    – 6v6gt
    Sep 15, 2023 at 13:24

1 Answer 1

1

In your code, you define the global variable ledState and initialize it to the value "LOW".

In the loop() function, the ledState variable is used to determine the (next) state of the LED once every 1000 milliseconds, like this:

...
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
  for (int i = 0; i < NUM_LEDS; i++) {
    myLeds[i] = CRGB::White;
    FastLED.show();
  }
} else {
  for (int i = 0; i < NUM_LEDS; i++) {
    myLeds[i] = CRGB::Black;
    FastLED.show();
  }
}
...

However, since your code does not change value of ledState to reflect the new state of the LED array, the next time the above if (...) comparison is run, the outcome is the same and the color of the LEDs not be changed.

The simplest change to make your LEDs blink as you intended, would be to change the above piece of code to the following:

...
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
  for (int i = 0; i < NUM_LEDS; i++) {
    myLeds[i] = CRGB::White;
    FastLED.show();
    ledState = HIGH;
  }
} else {
  for (int i = 0; i < NUM_LEDS; i++) {
    myLeds[i] = CRGB::Black;
    FastLED.show();
  }
  ledState = LOW;
}
...

An alternative (arguably more elegant) solution would be to declare ledState as a boolean (true/false) variable so that you can further simplify your code:

#include "FastLED.h"

bool ledState = false;    // ledState used to set the LED
long previousMillis = 0;  // will store last time LED was updated
long interval = 1000;     // interval at which to blink (milliseconds)

#define NUM_LEDS 10
CRGB myLeds[NUM_LEDS];

void setup() {
  FastLED.addLeds<NEOPIXEL, 4>(myLeds, NUM_LEDS);
}

void loop() {
  unsigned long currentMillis = millis();
  FastLED.setBrightness(25);
  
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (!ledState) {
      for (int i = 0; i < NUM_LEDS; i++) {
        myLeds[i] = CRGB::White;
        FastLED.show();
      }
    } else {
      for (int i = 0; i < NUM_LEDS; i++) {
        myLeds[i] = CRGB::Black;
        FastLED.show();
      }
    }
    ledState = !ledState;    // make ledState reflect the new LED state

  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.