Control RGB LED with IR remote

So I’m trying to make a rainbow effect with the RGB LED when I press the number 1 on my remote, but it doesn’t work. It keeps freezing at a certain colour, and after some time the led jumps to a different colour. The code was working when I made just the code for the rainbow effect without the IR remote and that works perfectly fine. I don’t know what is causing the problem. Any help would be appreciated. I’m a noob btw.

#include <IRremote.h>

int red = 255;
int green = 255;
int blue = 255;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int r = 0;
int g = 0;
int b = 0;
int msSpeed = 50; // milli seconds

const int REC_PIN = 7;
IRrecv irrecv(REC_PIN);
decode_results results;
unsigned long key_value = 0;
const int rainbow = 1;
int loopThis = 0;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  irrecv.enableIRIn();
  irrecv.blink13(true);
}

void loop() {
  start:
  if (irrecv.decode(&results)){
        if (results.value == 0XFFFFFFFF)
          results.value = key_value;
        switch(results.value){
          case 0x9716BE3F:      // 1 on the remote
          if((red == 255) && (green == 255) && (blue == 255)) {
            green = 0;
            blue = 0;
          }
          loopThis = rainbow;
          break;
          case 0x3D9AE3F7:      // 2 on the rmote
          loopThis = 0;
          red = 255;
          green = 255;
          blue = 255;
          RGB(red, green, blue);
          delay(50);
          break;      
        }
        key_value = results.value;
        irrecv.resume(); 
  }
  
  switch(loopThis){
    case rainbow:

    if((red == 255) && (green == 0) && (blue == 0)) {
      b = 0;
      g = 1;
    }
    if((red == 255) && (green == 255) && (blue == 0)) {
      g = 0;
      r = -1;
    }
    if((red == 0) && (green == 255) && (blue == 0)) {
      r = 0;
      b = 1;
    }
    if((red == 0) && (green == 255) && (blue == 255)) {
      b = 0;
      g = -1;
    }
    if((red == 0) && (green == 0) && (blue == 255)) {
      g = 0;
      r = 1;
    }
    if((red == 255) && (green == 0) && (blue == 255)) {
      r = 0;
      b = -1;
    }
    red = red + r;
    green = green + g;
    blue = blue + b;
    RGB(red, green, blue);
    delay(msSpeed);
    
    goto start;
    break;
  }
}

void RGB(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

I’ve also found that if I set all the RGB values to 254 instead of 255, the colour of the RGB LED is light blue. I’ve also tried with a couple of RGB LEDs, but the problem doesn’t go away.

if((red == 255) && (green == 0) && (blue == 0)) {

you have various cases for changing the increment values. are you sure you've covered every case?

what if red >= 255?

I dont think that's possible, because after red = 255, it goes down to 0 again. The rainbow effect did work perfectly on its own, but with the IR sensor code, it just doesn't work correctly.

for all cases, not just red

you have a problem ... can you prove all cases are handled

If you hook up a common cathode RGB LED to an Arduino and you upload this code, it should work. For me it does work.

int red = 255;
int green = 0;
int blue = 0;
int redPin = 11;
int greenPin = 10;
int bluePin = 9;
int r = 0;
int g = 0;
int b = 0;
int msSpeed = 50; // milli seconds

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {  
  if((red == 255) && (green == 0) && (blue == 0)) {
    b = 0;
    g = 1;
  }
  if((red == 255) && (green == 255) && (blue == 0)) {
    g = 0;
    r = -1;
  }
  if((red == 0) && (green == 255) && (blue == 0)) {
    r = 0;
    b = 1;
  }
  if((red == 0) && (green == 255) && (blue == 255)) {
    b = 0;
    g = -1;
  }
  if((red == 0) && (green == 0) && (blue == 255)) {
    g = 0;
    r = 1;
  }
  if((red == 255) && (green == 0) && (blue == 255)) {
    r = 0;
    b = -1;
  }
  red = red + r;
  green = green + g;
  blue = blue + b;
  RGB(red, green, blue);
  delay(msSpeed);
}

void RGB(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}