Using multiple solid colors for LED strip

I'm trying to put LED's into a bookshelf, but I want each of the shelfs to glow a different solid color. I'm having trouble writing a code where for example first 10 LEDS are Green and the next 10 are Red and stay static. This is variable since my shelf sizes are all different. I imagine I'll need like 16 colors over like 32ft. I'm currently experimenting with UNO R3 and 16ft of WS2812B. The code I've been attempting to write is below

#include <FastLED.h>
#define NUM_LEDS  20
#define LED_PIN   7


CRGB leds[NUM_LEDS];

void setup() {
 FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
 FastLED.setBrightness(50);
}

void loop() {

 for(int i = NUM_LEDS; i <= 10;i--){
   fill_solid (leds, i, CRGB::Red);
   FastLED.show();
}

   for(int i = NUM_LEDS; i >= 10;i++){  
     fill_solid(leds, i, CRGB::Green);
     FastLED.show();
 }
} 

  • Read this:
    for - Arduino Reference

  • Instead of trying to get the strip working just now, try printing to the Serial Monitor to see if your for( ) loop is doing what you think it is.

This:

Says:

  1. Start "i" at NUM_LEDS (20)
  2. While "i" is less than 10
  3. Decrement "i" (20, 19, 18...)

While this:

Says:

  1. Start "i" at NUM_LEDS (20)
  2. While "i" is greater than 10
  3. Increment "i" (20, 21, 22, ...)

So, "i" will increment forever because "i" will only be less than 10 in 32757 increments (when "int" overflows).

I think you wanted the second for() loop to count from 0 to 10.

[edit]... I just noticed the fill_solid... disregard the above...

"fill_solid" wants to know

  1. The array of LEDS - yours is "leds"
  2. Starting from LED "0", the number of LEDS to color
  3. The color.

If you want 0 - 9 to be RED and 10 - 19 to be GRN, you need to first "fill_solid" 0 - 19 green, then overwrite 0 - 9 with "fill_solid" 10 red.

I figured out how to do it with help from a friend

#include <FastLED.h>
#define NUM_LEDS  20
#define LED_PIN   7


CRGB leds[NUM_LEDS];

void setup() {
 FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
 FastLED.setBrightness(50);
}

void loop() {
  for(int i = 0; i < NUM_LEDS;i++){
    if(i<10){
      leds[i] = CRGB::Green;
    }
    if(i>=10&& i<15){//cubby 2
      leds[i] = CRGB::Red;
    }
    if(i>=15&& i<30){ //cubby 1
      leds[i] = CRGB::Magenta;
      
    }
    FastLED.show();
  }


}

That's not the solution because you were using fill_solid() and added a color... even though you were given the answer (but never used it).

#include <FastLED.h>
#define NUM_LEDS  20
#define LED_PIN   7

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
}

void loop() {
  fill_solid(leds, 20, CRGB::Magenta);
  fill_solid(leds, 15, CRGB::Red);
  fill_solid(leds, 10, CRGB::Green);
  FastLED.show();
}

Why 30 when you only have 20 LEDs?

it was a place holder since I planned to use expand this code to more LEDs. So you’ll have to change the #define NUM_LEDs to 30 for this to work for all 30 in the code.

Try the sketch in Post #5. See how it does what your friend suggested in three lines? This method (fill_solid()) is called pixel-plaining, used for large-scale, high-speed graphics, where you define an area, color-in the area, then move to the next plain.

Here is Post #5, where all you need to do is change the NUM_LEDS and the 1/2, 1/4, 1/4 is done for you...

#include <FastLED.h>
#define NUM_LEDS  30
#define LED_PIN   7

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(255);
}

void loop() {
  fill_solid(leds, NUM_LEDS * 1.0, CRGB::Magenta);
  fill_solid(leds, NUM_LEDS * 0.75, CRGB::Red);
  fill_solid(leds, NUM_LEDS * 0.5, CRGB::Green);
  FastLED.show();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.