FastLED Pulse of Color and then Fade

FastLED Pulse of Color and then Fade

The following was done on a 12x12 grid / matrix of WS1812B LEDs (made from a strip).  I got the code from Scott Kletzien in the FastLED Google+ community. I had to tweak it a little bit to get it to work with my LEDs, and I stumbled with the setRGB function -- I thought it was used the same as HSV settings, but I was wrong.  I have an explanation of how it works HERE.  It's also briefly explained in the code below.




/*
Put together & tested by:
Scottie Digital
 */
#include "FastLED.h"
#define NUM_LEDS 144  // # of LEDS in the strip
CRGB leds[NUM_LEDS];
#define DATA_PIN 6 // Output Pin to Data Line on Strip

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<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop()
{
   for(int i = 0; i < NUM_LEDS; i++ )
   {
   leds[i].setRGB(0,255,0);  // setRGB functions works by setting
                             // (RED value 0-255, GREEN value 0-255, BLUE value 0-255)
                             // RED = setRGB(255,0,0)
                             // GREEN = setRGB(0,255,0)
   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(20);  // This delay sets speed of the fade. I usually do from 5-75 but you can always go higher.
}




6 comments:

  1. Thank you for this tutorial! It really helped me learn the fadeLightBy function and slim my code down!

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

  3. I am new with codeing, it's very hard for me. I have a led strip with 8 LEDS on it. I want them to fade in and after that I want a quick blink and then the light should stay on.

    Can you help me please?

    ReplyDelete
  4. Great code, but looking to change colors after each fade cycle.

    ReplyDelete
  5. Thanks for sharing! The code works amazingly, and I used in one of my own projects until a slightly simpler method occurred to me:

    ```cpp
    void breathe() {
    int bright = beatsin16(60, 0, 255); // between 0-255 @ 60 bpm
    CHSV color = CHSV(0, 0, bright); // equiv. CRGB::White
    fill_solid(leds, NUM_LEDS, color);
    }
    ```

    ReplyDelete