RGB flashing color between while loops

I have three while loops, each brightening and dimming a color, and between each loop, it briefly flashes the color that was just dimmed. RGB is common anode.

Here is a video of the LEDs cycling through: https://youtu.be/YP554Z8-hXI

int G;
int R;
int B;


int audio;
int c;
int t;
int rn;
int gn;
int bn;
int r;
int g;
int b;

void setup() {
  Serial.begin(9600);

  G = 11;
  R = 10;
  B = 9;
  
  
  audio = 1024;
  c = 0;
  t = 0;
  rn = 0;
  gn = 0;
  bn = 0;
  r = 0;
  g = 0;
  b = 0;
  
  pinMode(G, OUTPUT);
  pinMode(R, OUTPUT);
  pinMode(B, OUTPUT);
  
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);

  analogWrite(R,255);
  analogWrite(G,255);
  analogWrite(B,255);
  
}

void loop() {

  rn = 0;
  gn = 0;
  bn = 0;

  while (rn <= 255)
  {
    rn = rn + 1;
    audio = 1024;//analogRead(A0);
    r = rn * audio/1024;
    analogWrite(R,(255-r));
    analogWrite(G,(255));
    analogWrite(B,(r));
    delay(50);
  }

  while (gn <= 255)
  {
    gn = gn + 1;
    audio = 1024;//analogRead(A0);
    g = gn * audio/1024;
    analogWrite(R,(g));
    analogWrite(G,(255-g));
    analogWrite(B,(255));
    delay(50);
  }

  while (bn <= 255)
  {
    bn = bn + 1;
    audio = 1024;//analogRead(A0);
    b = bn * audio/1024;
    analogWrite(R,(255));
    analogWrite(G,(b));
    analogWrite(B,(255-b));
    delay(50);
  }

}

Try

r = (long) rn * audio/1024;

t briefly flashes the color that was just dimmed.

This is because the last iteration of the while loop starts with a value of rn being 255. You then increment rn making it 256 which when you apply to PWM is the equivalent of zero. So make all the while loops just less than 255 not less than or equal to 255.

You are going to add an analog reading to this and at the moment it is commented out. But when it is not the your lines like

r = rn * audio/1024;

Are going to return zero because the variable audio is an integer, and any integer divided by a larger integer will return zero. You need to look at floating point variables at this stage.

Grumpy_Mike:
This is because the last iteration of the while loop starts with a value of rn being 255

Agreed, the OP could use for() loops for this instead of while() loops, or assign the loop control variable back to zero before the while() loop.

Grumpy_Mike:
But when it is not the your lines like

r = rn * audio/1024;

Are going to return zero because the variable audio is an integer, and any integer divided by a larger integer will return zero.

Mike, won't the * and / get evaluated in left to right order because they have equal precedence? Hence my suggestion of casting to long so that the intermediate result of the * did not overflow, and the / would then give a useable result. (I though this overflow was causing the effect the OP is describing, I did not spot the more obvious answer as you did!)

won't the * and / get evaluated in left to right order because they have equal precedence?

Maybe but it would be better to spell it out by using brackets, they don't add any penalty and they make it explicit what you want to do.

r =( (long)rn * (long)audio)/1024;

But in this case with a 50mS delay we are not in any hurry and the extra time for a floating point operation will not be a handicap.