rgb led flickering when

hey guys, Im doing a tutorial to make a push button switching color rgb led, here is the code

const int rled=9;
const int bled=11;
const int gled=10;

const int button=2;

boolean lastbutton = LOW;
boolean currentbutton = LOW;
int ledMode = 0;

void setup()
{ 
  pinMode(rled, OUTPUT);
  pinMode(bled, OUTPUT);
  pinMode(gled, OUTPUT);
  pinMode(button, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(button);
   if (last != current)
    {
     delay (5);
     current  = digitalRead(button);
    }
   return current;
}


void setMode(int mode)
 {
   if (mode == 1)
   {
     digitalWrite(rled, HIGH);
     digitalWrite(bled, LOW);
     digitalWrite(gled, LOW);
   }
   if (mode == 2)
   {
     digitalWrite(rled, LOW);
     digitalWrite(bled, LOW);
     digitalWrite(gled, HIGH);
   }
   if (mode == 3)
   {
     digitalWrite(rled, LOW);
     digitalWrite(bled, HIGH);
     digitalWrite(gled, LOW);
   }
   if (mode == 4)
   {
     analogWrite(rled, 127);
     analogWrite(bled, 127);
     analogWrite(gled, 0);
   }
   if (mode == 5)
   {
     analogWrite(rled, 0);
     analogWrite(bled, 127);
     analogWrite(gled, 127);
   }
   if (mode == 6)
   {
     analogWrite(rled, 127);
     analogWrite(bled, 0);
     analogWrite(gled, 127);
   }
   if (mode == 7)
   {
     analogWrite(rled, 85);
     analogWrite(bled, 85);
     analogWrite(gled, 85);
   }
   else (mode == 0);
   {
     analogWrite(rled, 0);
     analogWrite(bled, 0);
     analogWrite(gled, 0);
   }
   
 }


void loop()
{
  currentbutton = debounce(lastbutton);
   if (lastbutton == LOW && currentbutton == HIGH)
    {
      ledMode++;
    }  
   lastbutton = currentbutton;
   if (ledMode == 8) ledMode = 0;
   setMode(ledMode);
}

im using 220 ohm resistors on the leds using usb power, im getting flickering anytime i have two colors going at the same time, but when i have just red, green or blue on, their fine. anyone know what my problem is?

they seem to stop flickering when i set my pwm to 100 instead of smaller values...

anyone know why this would happen? is it cause of a crappy rgb led?

Edit: I caught it.

You do know what PWM stands for right? Pulse Width Modulation. The digital pins (with the ~ symbol using analogWrite() ) output a pulse based on a timer that takes a parameter value 0 - 255. The higher the number, the more the pulses come out HIGH, the lower the number, the more the pulses come out LOW.

So your flickering is due to those lower pulses. The way you would fix this issue is with a diode and capacitor.
The diode prevents backfeed and protects the arduino, and the capacitor stores the charge level.

Try it, a standard 1N4001 diode and a 10uf cap should be enough.