Hi folks, I'm very new to FastLED and Arduino,

Hi folks,

I’m very new to FastLED and Arduino, but I have a few questions I’m struggling with for a project I’m working on.

Is it possible to create palettes and gradient palettes at runtime? All the examples and documentation I’m seeing has these being setup with a define statement.

Similarly, is there an easy way to set the LED type at runtime? With FastLED using different syntax depending on the LED type I suspect I’m going to have to create a function to call the appropriate command to add the LEDs for each set.

What I’m trying to do is hard code as little as possible so my controllers can use multiple types of LED, various LED string lengths, and user defined palettes, but it seems this isn’t straightforward in FastLED.

Because of fastled’s design and history and priorities (performance, mainly) it currently requires a number of things to be determined at compile time. Building a generic controller is possible (there are a few folks here who have done that) - but it requires some wrangling to make work (and will increase the size of compiled code)

@Ross_Cooper-Smith This is a rather specific example, but here’s one way you can have a gradient palette you can change on the fly.

And you can update palettes on the fly too. Look at the FastLED ColorPalette example, lines 101-137. Each time those are called it’s setting up a palette. Feed them variables and change things on the fly.

I know others have examples they can share of this too.

Awesome, thanks for those. Can I create gradient palettes in the same way? Just by defining the variable and populating it at runtime?

Planning to run this on the ESP32, so hopefully I’ve enough power and resources in the IC to cope.

@Ross_Cooper-Smith For gradient palettes I believe you need to do something like my palette_example2.ino above, but then yes, you can update them whenever.

Also, have a look at this project that can run a few different types of strips. Might give you some ideas of how that’s possible.

Sometimes for a variable number of pixels it’s quick to just define the maximum you’d ever need, and then have a variable that you can change on the fly to specify the actual number currently being used (with that variable always being less then or equal to the max defined).

Thanks guys, I’ve been working through that, and learning a bunch of Arduino on my own. Do you think this approach would work?

//TODO: Load actual number of strips from settings
uint8_t numberLedStrips = 2;

//Structure to define LED strips
typedef struct ledArray_struct
{
String ledType;
String hardwarePins;
String colourOrder;
String ledArray;
uint8_t numberOfLEDs;
}
//Create an array to track all led strips
ledArray_struct ledStrips[numberLedStrips];

//TODO: Populate this with saved data for actual LED strip details
ledStrips[0].ledType = “SK9822”;
ledStrips[0].hardwarePins = “DATA_PIN_A,CLK_PIN_A”; //I believe we can use this structure to handle both 3 and 4 pin arrays
ledStrips[0].colourOrder = “BGR”;
ledStrips[0].ledArray = “leds_A”;
ledStrips[0].numberOfLeds = 7;

ledStrips[1].ledType = “SK9822”;
ledStrips[1].hardwarePins = “DATA_PIN_B,CLK_PIN_B”; //I believe we can use this structure to handle both 3 and 4 pin arrays
ledStrips[1].colourOrder = “BGR”;
ledStrips[1].ledArray = “leds_B”;
ledStrips[1].numberOfLeds = 7;

//Initialise all LED strips
for (int i = 0; i < numberLedStrips; i++) {
//Add LEDs
FastLED.addLeds<ledStrips[i].ledType,ledStrips[i].ledArrayPins,ledStrips[i].colourOrder>(ledStrips[0].ledArray,ledStrips[0].numberOfLeds);
//Ensure LEDs are off at start
fill_solid(ledStrips[i].ledArray,ledStrips[i].numberOfLeds),CRGB::Black);
}

FastLED.setBrightness(brightness);
FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS);
FastLED.show();