Interruptible fade? I’m doing a project where some motion triggers the LEDs to come on and then fade out, and I can’t figure out how to make the fade interruptible. How do I use fadeToBlackBy() without adding a delay?
One thing you could do is to use EVERY_N_MILLIS instead, which allows you to change the value on the fly. I prefer to put the show() in loop, so possibly something like this:
int timeval = 5;
bool aninterrupt;
void loop() {
// Other code here
EVERY_N_MILLIS_I(thisTimer,5) { // Sets original delay to 5, but allows it to be changed.
thisTimer.setPeriod(timeval);
fadeToBlackBy( leds, NUM_LEDS, fadeRate); // Fade out pixels.
}
LEDS.show();
} // loop()
void myinterrupt() {
if(aninterrupt) timeval = 1000; // Or other value.
} // myinterrupt()
In this case, if the delay is removed there are two remaining things that will effect the look (speed) of the fade: the time in the EVERY N MILLISECONDS, and the fadeRade.
I would start by increasing the EVERY N MILLISECONDS time a bit to 10 or 15, and then adjust fadeRate until you get a similar looking fade as before.
I may be missing something, but I usually just do something like this in loop:
fadetoBlackBy(leds, NUM_LEDS, fadeRate); // fade out pixels
if(condition) {
// show your color, pattern, animation, or whatever
}
FastLED.show();
This way, if not triggered, whatever is being shown will fade to black. The fade speed is controlled by the value of fadeRate. If triggered, the fade will be completely cancelled out by the pattern, color, animation, etc. This also gives cool motion trails to your animation, the length of which is controlled by fadeRate.
Also: project preview! I’m making sound and motion reactive Wonder Woman bracers. Right now they can only block ONE bullet every 10 seconds or so due to the un-interruptible fade, and bad guys seldom shoot just one bullet… so this is a problem. You may be saving Wonder Woman’s life with your code help. (Who ever said this was a useless hobby??)