Anybody has any idea how to change the NUM_LEDS during thr runtime?

fastled-support
gplus
(Maroti Piki) #1

Anybody has any idea how to change the NUM_LEDS during thr runtime?
It want to “add” leds with a button press.

(Marc Miller) #2

There are other threads about this and some have created a few work arounds, but the short answer is FastLED wasn’t designed to be able to do that.
The simplest work around is is to set NUM_LEDS to a max number of pixels you’ll work with, and then have a second variable that is less then or equal to NUM_LEDS that you vary while running that changes things in your code as needed.

(Andrew Tuline) #3

That’s what I do.

(Tod Kurt) #4

Yup, basically: “How much RAM do I have left / 3 = NUM_LEDS”

(Maroti Piki) #5

@Andrew_Tuline @Tod_Kurt
can you please share some example code? I have no idea how to solve this. Thanks!

(Tod Kurt) #6

No explicit example code except the built-in examples. You basically just have a variable that is “num_leds_configured” that must always be <=NUM_LEDS and use “num_leds_configured” to the FastLED functions.

Instead of this:
fadeToBlackBy( leds, NUM_LEDS, 20);
do:
fadeToBlackBy( leds, num_leds_configured, 20);

If you’re asking how to determine what the maximum NUM_LEDS can be, when you compile, take note what it says for “dynamic memory” used. For instance, with the FastLED “Demo100” sketch on an Arduino Uno, I see:
“Global variables use 373 bytes (18%) of dynamic memory, leaving 1675 bytes for local variables. Maximum is 2048 bytes.”

This means you have at most 1675 bytes more for LEDS. Since the “Demo100” sketch is already set up for 64 LEDs, you in theory have room for 1675/3 = 558 more LEDs. In practice you don’t get to use all that remaining memory. I usually reduce it by 30% or so, but it really depends on how you write your code.

If you change “Demo100”’ NUM_LEDS to 500, you get the message:
“Global variables use 1681 bytes (82%) of dynamic memory, leaving 367 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.”

If you use Arduino Strings and use Serial.print() a lot, this warning is good to heed. Those things can use up RAM in non-obvious ways. And besides, 500 LEDs is a lot to handle on an Arduino Uno. You’ll likely have other bigger issues to contend with before even 100 LEDs is a problem.

(Andrew Tuline) #7

In my case:

#define MAX_LEDS 48

uint8_t NUM_LEDS;

Later on in the code, I’ll be referring to NUM_LEDS in the routines as usual, and will ALWAYS make sure it’s <= MAX_LEDS.

If you’re interested in a bit more, my seirlight demo stores that length in EEPROM which can be changed via controller:

(see line 379 for the EEPROM read operation and line 531 for the write operation).