0

I am fearly new to Arduino and FastLED. I found a nice Sketch which fits almost perfectly to my needs.

Just one Problem:

I dont want the Sketch to effect ALL LEDs in one color.
I want some LEDs in Red (ex. LEDs 0-4) Some in green and others in blue.

They could all have the same effect and timing, but it would be really neat if I could make them out of sync a little bit.

But having the possibility to make different LEDs different colored would be great. Here is the "Pulse" or "Breath" Sketch I want to use.

    #include <FastLED.h>

    #define NUM_LEDS 37
    #define DATA_PIN 11
    #define CLK_PIN 13

    CRGB leds[NUM_LEDS];

    int fadeAmount = 5;  // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.
    int brightness = 0; 

    void setup()
    {
      FastLED.addLeds<APA102, DATA_PIN, CLK_PIN, BGR>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
      FastLED.setBrightness(50);
    }

    void loop()
    { 
       for(int i = 0; i < NUM_LEDS; i++ )
       {
       leds[i].setRGB(0,255,250);  // Set Color HERE!!!
       leds[i].fadeLightBy(brightness);
      }
      FastLED.show();
      brightness = brightness + fadeAmount;
      // reverse the direction of the fading at the ends of the fade: 
      if(brightness == 0 || brightness == 255)
      {
        fadeAmount = -fadeAmount ; 
      }    
      delay(80);  // This delay sets speed of the fade. I usually do from 5-75 but you can always go higher.
    }

1 Answer 1

3

The colors of the LEDs in your sketch are set in this loop:

for(int i = 0; i < NUM_LEDS; i++ )
{
    leds[i].setRGB(0,255,250);  // Set Color HERE!!!
    leds[i].fadeLightBy(brightness);
}

You loop over all LEDs and set them to a specific color and brightness. The changes will be send to the LEDs, when you call FastLED.show() afterwards. There is nobody, that prevents you from setting a different color for each LED or to only chnage the value for some of the LEDs.

For example, to light the first 5 LEDs in red, the next 10 in green and leaving the rest as they were before, you can use this code (Just exchange it with your for loop):

for(int i=0;i<5;i++){
    leds[i].setRGB(255,0,0);
    leds[i].fadeLightBy(brightness);
}
for(int i=5;i<15;i++){
    leds[i].setRGB(0,255,0);
    leds[i].fadeLightBy(brightness);
}
FastLED.show();

The principle is, that you have an array with color values. Each value consists of 3 smaller values for red, green and blue (RGB)(The color results from the distribution between red, green and blue; the brightness results from the overall level of red, green and blue). And each value of the array is bound to an LED (First value to first LED, second value to second LED,...). You can change the color values in the array as you like. And when you want the LEDs to show the current values in the array, you send them out by using FastLED.show(). This means you have to call this function everytime, that you want LEDs to change. You don't have to change every value in the array for this to work.

If you would like to build an asynchronous change of colors, you can ditch the delay(80) and use non-blocking code like in the BlinkWithoutDelay example of the Arduino IDE. There you use the millis() function as a clock (it returns the milliseconds since the Arduinos startup). You take a timestamp at the beginning and compare the difference between it and the current time:

unsigned long timestamp=0;
unsigned long interval=80; //interval of 80 milliseconds

void loop(){
    unsigned long current_time=millis();
    if(current_time - timestamp > interval){
        // do something here, like setting LED colors
        timestamp = current_time;
        // don't forget FastLED.show(), to output the colors
    }
}

You can use multiple of these timestamps with if-statements, one for each segment you want to change in a different frequency.

2
  • Hi, wow! I am absolutely amazed how good you explained and showed all this to me! This is great! I will absolutely try this when i am at home from work and give feedback. Thank you very much ! Commented Sep 14, 2018 at 12:20
  • WOW this worked perfect!! Again: Thank you very much!!! Commented Sep 14, 2018 at 19:24

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.