IR Remote RGB LED sketch switches b/w colors, but gets stuck in while loops

Hello all. Very new to programming. I created a program to map out colors to various buttons on remote control, which worked great. You push the button, it turns red, push another button and it turns green, etc. Then I tried adding a new function where you press a button and have the RGB cycle through a number of while loops to brighten and dim between the colors. However, when I select this function the program just keeps going through the while loops and the other buttons no longer function as expected. I'm not sure if I need to add in IR.resume commands after each while loop or what. Any help would be appreciated. I've attached the code below. Thank you.

#include <IRremote.h>
const int IRpin=5;
const int redPin=6;
const int greenPin=9;
const int bluePin=10;//pwm pin 11 reserved by IRremote library for timer
                     //and can't be used for analog writing

IRrecv IR(IRpin); //creating IR object 'IR'
decode_results cmd;    //library specific command

int brightVal=255;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
IR.enableIRIn();
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
while(IR.decode(&cmd)==0){}  //call IR.decode function and cmd function 
delay(1500); //delay to initialize IR receiver

if(cmd.value==0xFF38C7){ //power on - ok button
analogWrite(redPin,255);
analogWrite(greenPin,255);
analogWrite(bluePin,255);
delay(1500);
}
if(cmd.value==0xFFB04F){ //power off - # button
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
delay(1500);
}
if(cmd.value==0xFFA25D){ //red - 1 button
analogWrite(redPin,brightVal);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
delay(1500);
}
if(cmd.value==0xFF629D){ //green - 2 button
analogWrite(redPin,0);
analogWrite(greenPin,brightVal);
analogWrite(bluePin,0);
delay(1500);
}
if(cmd.value==0xFFE21D){ //blue - 3 button
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,brightVal);
delay(1500);
}
if(cmd.value==0xFF6897){ //Rainbow- * button
    digitalWrite(redPin,LOW);
  digitalWrite(greenPin,LOW);
  digitalWrite(bluePin,LOW);
int brightness=0;

while(brightness<256){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness-5;
} 
while(brightness<256){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness-5;
} 

while(brightness<256){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness-5;
} 
while(brightness<256){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<256){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness-5;
}  
}
IR.resume();
}

Once in a while loop you are stuck there until the statement is false. No other code will run. When you use delay no other code will run. A combination of the two means that nothing else will work for the duration it takes to complete the loops and delays unless something happens to interrupt it. Then you will have a millisecond or 2 to detect an input before it loops again.

Probably best to try and code with as few while loops and delays as possible. You want the code to loop quickly and not be delayed so that it can pick up inputs. Otherwise you need to poll for inputs in the while loops.

That’s my understanding anyway

do you know which while loop it gets stuck in? you can add a serial print to identify it

or is it just taking longer than you expect?

I just realized how awful that code is. I removed all the extra while loops. Basically the issue is I want to have the RGB fluctuate between colors and brightness if I press a button until I press another button. The problem is that if I add an IR.resume() function, or break inside the last while loop it stops the function: the light just goes low. If I don't have a break or IR.resume() function, the program just becomes trapped in the fluctuating portion of the program.

#include <IRremote.h>
const int IRpin=5;
const int redPin=6;
const int greenPin=9;
const int bluePin=10;//pwm pin 11 reserved by IRremote library for timer
                     //and can't be used for analog writing

IRrecv IR(IRpin); //creating IR object 'IR'
decode_results cmd;    //library specific command

int brightVal=255;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
IR.enableIRIn();
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
while(IR.decode(&cmd)==0){}  //call IR.decode function and cmd function 
delay(1500); //delay to initialize IR receiver

if(cmd.value==0xFF38C7){ //power on - ok button
analogWrite(redPin,255);
analogWrite(greenPin,255);
analogWrite(bluePin,255);
delay(1500);
}
if(cmd.value==0xFFB04F){ //power off - # button
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
delay(1500);
}
if(cmd.value==0xFFA25D){ //red - 1 button
analogWrite(redPin,brightVal);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
delay(1500);
}
if(cmd.value==0xFF629D){ //green - 2 button
analogWrite(redPin,0);
analogWrite(greenPin,brightVal);
analogWrite(bluePin,0);
delay(1500);
}
if(cmd.value==0xFFE21D){ //blue - 3 button
analogWrite(redPin,0);
analogWrite(greenPin,0);
analogWrite(bluePin,brightVal);
delay(1500);
}
if(cmd.value==0xFF6897){ //Rainbow- * button
    digitalWrite(redPin,LOW);
  digitalWrite(greenPin,LOW);
  digitalWrite(bluePin,LOW);
int brightness=0;

while(brightness<255){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(redPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<255){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(greenPin,brightness);
  delay(20);
  brightness=brightness-5;
}
while(brightness<255){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness+5;
}
while(brightness>0){
  analogWrite(bluePin,brightness);
  delay(20);
  brightness=brightness-5;
IR.resume();                              //added to break out of the while loops
}  
}
IR.resume();
}

Look up state machines. You don’t need a while loop because the program already loops. You just need to have a variable controlling the state And your if statements can do the rest.