RGB LED Fade and chang color in succession

Ok, I have an RGB LED hooked up to my Arduino on pins 9, 10, and 11. I have tried all day piece together the code with little avail. What I want the RGB to do is to start black then fade to red then back to black. Then I want it to fade from black to green and back to black. Finally, I want it to fade from black to blue and back to black, and then repeat the loop. Thank you very much. :slight_smile:

Look under Files=>Examples=>Basics=>Fade on the IDE, it's just what you need except that you have to swap pins when the LED has been completely faded to off.

Here's my attempt at changing it to fit your situation:

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

// pins for RGB
int ledRed = 9;
int ledGreen = 10;
int ledBlue = 11;
int led = ledRed;

//int led = 9;           // the pin that the LED is attached to



int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // if done fading down switch colors on RGB
  if (brightness == 0 )
  {
    // advance led pin to next color
    if( led == ledRed )
    {
      // red -> green
      led = ledGreen;

    } 
    else if( led == ledGreen ) {
      // green -> blue
      led = ledBlue;

    } 
    else {
      // blue -> red
      led = ledRed;

    } // else

  } // if

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

Thank for your help Blue Eyes. I will try it out.