Outside of the main loops and declared ready to be used. What if we wanted to run with an LED count that’s stored in EEPROM, how do we go about declaring these initial variables?
My experience with EEPROM is limited, so your answer is probably found somewhere else, but what’s the reason for wanting to store it there? You’d need extra code to first write to, then read from before you could use it. I’ve only ever used EEPROM to store variables to “pick up where the program left off” between power cycles.
@Christopher_Kirkman1 because I want something to be configurable without reflashing it everytime - in this case the LED count.
I already use the EEPROM for some other things and have plenty of space spare, just wondering how to actually do this declaration, everything else is written already.
Put this after the FastLED.h include:
CRGB* leds = 0;
Put this inside setup():
int numLEDs = NUM_LEDS; // get this number from wherever
leds = (CRGB*) malloc((sizeof(leds)/sizeof(leds[0]) * numLEDs));
I’d recommend only doing this once per startup. Make people reset the device to set the new array length. malloc() on C makes people uncomfortable but it should be fine if you’re only running it once per startup.
If a "#define’ constant can be replaced with something else depents also on how the defined constand is used in your sourcecode.
Basically all expressions in the source code that start with a ‘#’-sign are preprocessor instructions for the compiler. This means that a routine called the preprocessor processes all those expressions in the sourcecode immediately before the source code gets compiled. Or in other words: the sourcecode will become modified based on those instructions before it gets compiled. The defined constants will be handled in a kind of ‘search and replace’ like you would do within a text processing application like word. Sometimes you can find #if#else#endif which is used for a conditional replacement of code segments.
Therefore an expression like ‘CRGB leds[ NUM_LEDS ]’ will be translated into ‘CRGB leds[ 160 ]’.
It gets critical when it comes to the so called token pasting operator:
Example:
#define index 3
…
result = var##index + fakt##index;
Will be translated into…
result = var3 + fakt3;
and of course the compiler will throw an error if the created expressions var3 or fakt3 are not somewhere declared;
If such a preprocessor constant is used inside a predefined library one can never be sure if it is used in such a way. If so, it can hardly become replaced with values stored inside EEPROM.
I’ve kept the NUM_LED’s as a max (renamed to MAX_LED’s), and I’m now using NUM_LED’s as the stored version. This allows me to run it at runtime without issues, but use any amount of LED’s I require (up to 50 is safe in this case on an ATTiny85 without memory issues (ish). Thanks for the suggestions guys, I’ll stick with this method for now, it felt a bit wrong doing it, which I think is why there’s basically no examples around (aside from the one found above so thank you for that!).