Go Down

Topic: redefine NUM_LEDS (FastLED.h) in setup() (Read 208 times) previous topic - next topic

Paloik

May 07, 2014, 02:09 pm Last Edit: May 07, 2014, 02:15 pm by Paloik Reason: 1
Hey, i'm currently working on a project to control a LED-strip (NEOPIXEL) with a rotary-switch (with push-button function).
To adress the LEDs i've includet FastLED.h, to encode the rotary, i includet the library Encoder.h.
Everything works almost well apart from the push-button, but thats a problem for an other thread!

Now i tried to optimize my sketch to (re-)set the "NUM_LEDS" only by "serial.input()" in "void setup()", but i only get a miss match of LED-adressing.

I know the problem is that i try to redefine the global constant in the setup()-function and this causes the failure, but i dont know why! I'm very new in arduino Cc-programming and not very firm with it.

In my code i set NUM_LEDS as global with
Code: [Select]

#include <FastLED.h> // use LED-library to adress the used LED-strip correctly
const byte NUM_LEDS = 75; // similar to #define NUM_LEDS 75
CRGB leds[NUM_LEDS]; // LED color-schema

and everything is fine.

Next, i re-defined NUM_LEDS in void setup() (at this point without serial-input read, its not  necessary for testing)
Quote

#include <FastLED.h>   // use LED-library to adress the used LED-strip correctly
const byte NUM_LEDS = 75; // similar to #define NUM_LEDS 75
CRGB leds[NUM_LEDS];   // LED color-schema
...
...
void setup() {
   #ifdef NUM_LEDS
   #undef NUM_LEDS
   #endif

   #define NUM_LEDS 75
   ...
   ...
}
...

But with this config, the LED-adressing is miss matched. All LEDs light up and do anything else than it should do.

How can i reset / redefine NUM_LEDS in a correct way? I need the redefinition because i want to change the amount of LEDs dynamic and tell the amount at arduino startup.
Maybe, is there a way the to read serial input before the NUM_LEDS has to be defined and setup() get initialised ?

Attached the whole sketch


Thanks for every help.
Any other code-optimizations are welcome too :)

Go Up