Hello,
Broadly put, I have been trying to make the LEDs of some buttons fade in when pressed and out when released.
I'm using:
RPi 3
Arduino Nano
WS2812 LED modules (individual, not strip, should that matter)
Microswitches wired to an interface that's connected via USB to Pi.
This is part of a larger setup in which I read when buttons are pressed and released on the Pi and send that information via i2c using the method described here:
Im sending it as a string that contains the button number and a "p" or "r" for press and release respectively(ex: p_01, r_01):
As everything along the path down to the Fade functions work as intended, I have isolated the issue to the method i have been trying to use for the actual animation (fade in/out).
The functions for both animations each consist of a for loop:
void FadeIn(int button, int red, int green, int blue) {
float r, g, b;
int btn = temp[4] - 48;//temp contains the code received from the Pi
Serial.println(btn);
for (int k = 0; k < 256; k = k + 4) {
if (temp[2] == 'r') {//if release signal
if (btn == button) {//check button number
Serial.println("break");
break;
}
} else {
r = (k / 256.0) * red;
g = (k / 256.0) * green;
b = (k / 256.0) * blue;
setAll(button, r, g, b);
strip.show();
}
}
}
Now, FadeOut is the same, only reversed the loop.
This all works well while pressing a single button. It also works well while quickly pressing multiple buttons as long as I remove those for loops and just print the event thats supposed to occur on the serial monitor.
Once I quickly press multiple buttons while using the for loops for the animation. It doesnt record most presses and releases and only plays the animation chaotically every few triggers and sometimes one of the LEDs freezes turned on until i press it's respective button again. I know the problem is probably using for loops as i think the execution gets stuck in it for a bit, but I'm not sure which other approach I could use for this.
Id greatly appreciate a push in the right direction.