Code doesn't work without Serial.println

So I’m creating a code that uses a rotary encoder to cycle through all of the colors in the RGB color spectrum at full brightness. I wanted to create this section of code that does that when the code turns on independent of the encoder to give the product a sort of “configuration” feel at the beginning of the code. To do this, I created a function that does it and included it at the end of the setup code. I plan on using this later in the loop code.

The problem is that the function doesn’t work unless I have a Serial.println() line in each while loop. Here is the code:

void configure(){
  r=255;
  g=0;
  b=0;
  while (g<255){
    Serial.println(g);
    analogWrite(9, r);
    analogWrite(10, g);
    analogWrite(11, b);
    analogWrite(3, r);
    analogWrite(5, g);
    analogWrite(6, b);
    g+=1;
  }
  while (r>0){
    Serial.println(r);
    analogWrite(9, r);
    analogWrite(10, g);
    analogWrite(11, b);
    analogWrite(3, r);
    analogWrite(5, g);
    analogWrite(6, b);
    r-=1;
  }
  while (b<255){
    Serial.println(b);
    analogWrite(9, r);
    analogWrite(10, g);
    analogWrite(11, b);
    analogWrite(3, r);
    analogWrite(5, g);
    analogWrite(6, b);
    b+=1;
  }
  while (g>0){
    Serial.println(g);
    analogWrite(9, r);
    analogWrite(10, g);
    analogWrite(11, b);
    analogWrite(3, r);
    analogWrite(5, g);
    analogWrite(6, b);
    g-=1;
  }
  while (r<255){
    Serial.println(r);
    analogWrite(9, r);
    analogWrite(10, g);
    analogWrite(11, b);
    analogWrite(3, r);
    analogWrite(5, g);
    analogWrite(6, b);
    r+=1;
  }
  while (b>0){
    Serial.println(b);
    analogWrite(9, r);
    analogWrite(10, g);
    analogWrite(11, b);
    analogWrite(3, r);
    analogWrite(5, g);
    analogWrite(6, b);
    b-=1;
  }
}

The code doesn’t work without the Serial.println() lines. Also, the led starts shining at around a green-yellow color when it’s supposed to start on red.

I’m coding this on a nano with the intention of putting this on a pcb.

I honestly have no idea why it’s doing this.

The Serial.println() lines are acting as short delays. Without them the code runs so fast you can’t see the colors changing.

(deleted)