FastLED fill_gradient function

FastLED fill_gradient function

As I mentioned earlier, I don't really know what I'm doing with FastLED as far as the LED suit goes.  However, I thought I would record what I have learned in case anyone noob-er than me might be helped out.

I'm pretty sure I'll want to assign specific colors to specific LEDs on the suit, so I've been looking for a FastLED function to do that.

There's a fill_solid function (which I'll cover later) that let's me start at LED-0 and go to LED-whatever-number-I-choose, but I haven't figured out a way to start anywhere other than LED-0.

Then I found...

FastLED fill_gradient


An example of it looks like this...

 fill_gradient(leds,0,CHSV(0,255,255),100,CHSV(96,255,255),SHORTEST_HUES); 

In the above example:

  • leds = I don't know why this is there or what it does, but it shows up in a lot of functions.
  • 0 = the starting LED.  Remember that the LEDs are indexed to zero, so LED #1 is identified as 0, LED #2 is identified as 1, and so on.
  • CHSV(0,255,255) = the starting color of the gradient expressed in HSV values, in this case red.
  • 100 = the ending LED.  Like the starting LED, it can be any number.
  • CHSV(96,255,255) = the ending color for the gradient, in this case green.
  • SHORTEST_HUES = The hue is a value on a color wheel.  There are always two directions you can go to get from one place on a wheel to another (one hue value to another hue value).  FastLED lets you pick the direction you want to go.  SHORTEST_HUES is the shortest distance between the two values (the most common choice), LONGEST_HUES is the longest way around.  FORWARD_HUES always moves clockwise.  BACKWARD_HUES always moves counter-clockwise.

In the above example, LEDs 0-100 will start as red at 0 and transition to green by 100.

Until I find a better way, I'll be using this function to color blocks of LEDs in one color, by choosing that color for the beginning and end of the gradient.

I wrote a quick sketch that has red and green blocks of LEDs and then reverses the colors, and repeats.



#include "FastLED.h"

#define NUM_LEDS 480 //the number states how many LEDs in total on your strip

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 6  // the Arduino pin you're connecting to.
#define CLOCK_PIN 13 //I haven't needed Clock Pin yet, so I don't have a clue!

// Define the array of leds
CRGB leds[NUM_LEDS]; //I know you need this, but don't know what for.

void setup() {
            FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
//the above line is telling FastLED what kind of strip you have.  Neopixel works for most.
     }


void loop() { //the stuff in void loop is the stuff that FastLED runs.

    fill_gradient(leds,0,CHSV(0,255,255),100,CHSV(0,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
    fill_gradient(leds,101,CHSV(96,255,255),200,CHSV(96,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
    fill_gradient(leds,201,CHSV(0,255,255),300,CHSV(0,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
    fill_gradient(leds,301,CHSV(96,255,255),400,CHSV(96,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
       delay(1000); //then we let the colors sit there for about a minute
     
        fill_gradient(leds,0,CHSV(96,255,255),100,CHSV(96,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
    fill_gradient(leds,101,CHSV(0,255,255),200,CHSV(0,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
    fill_gradient(leds,201,CHSV(96,255,255),300,CHSV(96,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
    fill_gradient(leds,301,CHSV(0,255,255),400,CHSV(0,255,255),SHORTEST_HUES); 
//the above sets up the leds, and the below shows them
    FastLED.show();
       delay(1000);  //then we let the colors sit there for about a minute

// the void loop starts all over again.

   
}


Again, the above is probably a very clunky way to accomplish the effect, but it's the best I've come up with so far.

If you know a better way and can explain it simply, or if you can explain some of the unexplained stuff above in simple language, please do so in the comments.  Thanks!

And please don't ask me questions or for help in solving problems -- I know next to nothing about this.  The FastLED Google+ community is very helpful though!

6 comments:

  1. You wrote that you did not know what 'leds' is or what it does. It is simply a reference to the CRGB leds[NUM_LEDS]; array you defined on line 13. CRGB leds[NUM_LEDS] creates an array of type CRGB (red, green, blue values) of length NUM_LEDS. Hope this helps. Great job!

    ReplyDelete
  2. You're using Fill_Gradient to fill sold blocks of colour. You can do that much more simply with the Fill_Solid command. Fill_Gradient will blend a starting colour and an end colour between a start and end led position. It is just a matter of altering the CHSV values above.

    ReplyDelete
  3. I got something out of this. Cheers.

    ReplyDelete
  4. As far as I know new colors - as in rainbow example - are always enter at led 0, the first one on the string. Maybe someone can tellme how to change the direction so that new colors enter at the end of the string and move downwards.

    ReplyDelete
  5. This isn't super well explained, and I'm also pretty noobish, but it sort of shows up in the documentation here: https://github.com/FastLED/FastLED/wiki/Controlling-leds

    You can write:
    fill_solid( &(leds[i]), 1, CRGB( 255, 68, 221) )

    where "i" can be changed to whatever you want the starting LED to be. You're making a reference to the LED array at a different starting position.

    ReplyDelete
  6. Im doing the same for my Xmas lights, but partially fill a strip with solid colors using:
    fill_solid( &(strip1[0]), 10, CRGB::Blue );
    fill_solid( &(strip1[11]), 15, CRGB::Green );
    where "strip1" is my definition of strip 1 or strip 2 etc.
    where [0] in the first line is the starting LED id on that strip for Blue LEDs and [11] is the start LED for the Green LEDs. The Blue LEDS have a length of 10 pixels, and the Green LEDs have a length of 15 pixels. I also use CRGB::RED etc as its much easier to define the colours I want as simple HTML colours.

    ReplyDelete