Hi all,
New to the group and still relatively new to code and microcontrollers. I’m looking for help on an interactive project I’m working on. I’m working on an LED project that is responsive to a fast vibration sensor with an Arduino Uno. Above a certain threshold, the LEDs (WS2812B LED Strip) all turn on at the same time for a set period of time. After that, the LEDs turn off.
It seems simple but I’m trying to figure out a simple way to have the LEDs fade to black rather than just shutting off. I’m having a difficult time (with my limited knowledge of code) figuring out what would work with the code I have right now. It’s taken me a while to get to this point and I’m trying not to get frustrated. If anyone could spare some FastLED wisdom, I would love the help. Thanks in advance!
Hi @Adriana_Onate , remove the instruction FastLed.clear(). You can use the function setBrigthness. One advice: never use delay(), your arduino don’t work during this time.
@Solo_Han_Solominator I want the LEDs to turn off after about 3 or 4 seconds but I want them to fade out rather than just shut off. If I get rid of FastLED.clear() they do not turn back off or fade out.
@Patrick_Aversenq Thanks for the advice, what can I use instead of delay() to keep the LEDs on for about 4 seconds when the threshold is met? If I delete FastLED.clear() the LEDs do not turn back off once they’ve been activated.
You’re not accessing the array properly. leds[NUM_LEDS] is actually a single location just past the end of the array, where some other variable may be.
You want to loop through the array like this (starting at 0 and stopping just before NUM_LEDS):
for(int i=0; i<NUM_LEDS; i++){
leds[i].fadeToBlackBy( 8 );
}
And then you also need to remove FastLED.clear(), because that immediately clears the leds array (to black).
I suggest you have a look at some coding tutorials regarding arrays if you don’t understand.
Your welcome, with this function you will continue to read the vibration during the fade. If the threshold is passed, you can turn on the light before the end of the fade. It will be your first multithreading code.
@Patrick_Aversenq I was able to get it working and its so much smoother! Thank you so much for giving me direction. All the basic tutorials start off with delays so I had no idea they were so crappy. Thanks!
By the way, delay() is fine if you don’t need to be doing anything else. FastLED.delay() is slightly better since it can give you dithering (fast flipping between 2 brightness levels to give you an in between brightness), but it’s not necessary in many cases.