Hi there, I am using FastLED library to make some LEDs rainbow fade however if I increase my variable Color before the 'for loop' it gives eratic values, however if I increase it after the 'for loop' it works normally, why is this?
If I increase it before the loop I get color values of:
Erratic LED Colors: 222, 166, 17, 211, 137, 1, 254
May be these bytes in memory were not used, in which case there could be not bad side effect* but if those bytes were the location of another variable, then you've overwritten this memory and changed something ...
*but the C++ standard says the writing beyond the end of your array is Undefined Behaviour, so all bets are off at that point, the fact that it works might change if you add one line of code
Awsome, thank you that is very interesting. I would have thought the IDE would have picked up on this kind of mistake. But it is really helpful to know now for future errors as I have had unexpected behavior from projects before.
It does, if you have the preferences set to show all compiler warnings (although the warning can be a bit cryptic):
/home/user/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino/main.cpp: In function 'main':
/home/user/Arduino/libraries/FastLED/src/hsv2rgb.cpp:467:11: warning: iteration 3 invokes undefined behavior [-Waggressive-loop-optimizations]
rgb.r = r;
^
/tmp/arduino_modified_sketch_325038/BlinkWithoutDelay.ino:18:20: note: within this loop
for(int i = 0; i <= NUM_LEDS; i++){
^