Arduino multiple button LED lighting animation issue

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.

how is "temp []" initialized?

it looks like a dynamic variable that will have random values from whatever is on the stack

I have already tested the contents of temp and it is working correctly. My issue is the for loop seemingly delaying my execution. But here's that part too.

volatile boolean receiveFlag = false;
char temp[32];

void loop(){
 if (receiveFlag == true) {
    //   Serial.println(temp);
    receiveFlag = false;

    if (temp[2] == 'p') {
      if (temp[4] == '0') {
        Serial.println("btn 0");
        FadeIn(0, 0, 255 , 0);
        strip.show()
}
else if (temp[2] == 'r') {
      if (temp[4] == '0') {
        Serial.println("btn 0");
        FadeOut(0, 0, 255 , 0);
        strip.show();
}
}
void receiveEvent (int howMany) {
  for (int i = 0; i < howMany; i++) {

    temp[i] = Wire.read();

  }
  for (int i = 0; i < howMany; ++i)
    temp[i] = temp[i + 1];

  receiveFlag = true;
}

Basically I use that to hold the current button press and access the identifiers i had set up in the sent strings, which does work as i need it.

my mistake

since you're problem depend on the timing of your button presses, could your fade routine be taking a non-insignificant amount of time.

i would have implemented fade in a way that it can be called repeatedly in loop() which is also checking for button presses and fade() would check a timestamp to determined when to do something

when i properly indent your code i get the following

volatile boolean receiveFlag = false;
char temp[32];
void loop(){
    if (receiveFlag == true) {
        //   Serial.println(temp);
        receiveFlag = false;
        if (temp[2] == 'p') {
            if (temp[4] == '0') {
                Serial.println("btn 0");
                FadeIn(0, 0, 255 , 0);
                strip.show()
            }

            else if (temp[2] == 'r') {
                if (temp[4] == '0') {
                    Serial.println("btn 0");
                    FadeOut(0, 0, 255 , 0);
                    strip.show();
                }

            }

            void receiveEvent (int howMany) {
                for (int i = 0; i < howMany; i++) {
                    temp[i] = Wire.read();
                }

                for (int i = 0; i < howMany; ++i)
                temp[i] = temp[i + 1];
                receiveFlag = true;
            }