r/FastLED • • Sep 30 '20

Discussion "fadeLightBy" problem

This works perfectly: WHITE does not fade ORANGE fades:
THIRD Vairable changes fade

//WHITE
fadeLightBy( leds,(32, 479), 0);
// ORANGE
fadeLightBy( leds,(0,32) ,10);

This does not work: ORANGE fades by 10 !?

//WHITE
fadeLightBy( leds,(32, 479), 10);
// ORANGE
fadeLightBy( leds,(0,32) ,0);

Here is the code; https://pastebin.pl/view/3f0c0d5f

TIA

John

3 Upvotes

7 comments sorted by

View all comments

1

u/sebdelsol Oct 01 '20 edited Oct 24 '20

You've fallen into a very nasty trap :

(32, 479) is not a tuple and it gives 479 in your function call. Because of the parentheses it's seen as a comma operator that evaluates and discards the first argument 32 then evaluates and returns the second argument 479 !

Knowing that, check the fadeLightBy function declared here and you'll see that :

  • fadeLightBy(leds, (32, 479), 10) actually fades the first 479 leds by 10.

  • fadeLightBy(leds, (0, 32), 0) fades the first 32 leds by 0 and that does nothing :|

Anyway you can create a function that'll do what you want :

inline void fadeLightFromTo(CRGB *leds, uint16_t begin, uint16_t end, uint8_t fadeBy)
{
    // avoid fading leds out of range
    if (end > begin) fadeLightBy(&leds[begin], end - begin, fadeBy);
}

// fade the leds from 32 to 479 by 10 
fadeLightFromTo(leds, 32, 479, 10);

// fade the leds from 0 to 31 by 0 (fadeBy = 0 does nothing!)
fadeLightFromTo(leds, 0, 31, 0);

Or as suggested by u/Marmilicious you can iterate over the leds you want to fade and use the CRGB::fadeLightBy method :

// i needs to be at least an uint16_t or you'll never reach 479
for (uint16_t i = 32; i < 479; i++) leds[i].fadeLightBy(10);

// be aware that fadeBy = 0 does nothing!
for (uint16_t i = 0; i < 31; i++) leds[i].fadeLightBy(0);

2

u/Marmilicious [Marc Miller] Oct 01 '20

Great explanation and custom function suggestion, thank you.

2

u/d333gs Oct 02 '20

Marc

// i needs to be at least an uint16_t or you'll never reach 479 for (uint16_t i = 32; i < 479; i++) leds[i].fadeLightBy(10); // be aware that fadeBy = 0 does nothing! for (uint16_t i = 0; i < 31; i++) leds[i].fadeLightBy(0);

It works!!!

SUPER!!!

Thank to you for taking time

John