My First Sketch: PWM Common Annode RGB Led 7 Color Generator

I picked up my first Arduino and I am impressed with it's capabilities. Just thought I'd share my first sketch. It uses the common annode, full color LED, Radio Shack model # 276-028. I used three PWM outputs to fade out each of the 7 basic color combination that can be made with the RGB LED. I am sure this code could be done simpler but I had so much fun getting it to work so easily! I can't wait to think up more things to make!

// RGB 7 Color Fade Out Looper
// By: CLM3S

// Define LED Color Pin Outputs
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;

void setup() 
{ 
} 
 
void loop() 
{ 

  // Fade Out Red
  for (int x=0; x<256; x=x+5) {
    analogWrite (RedPin, x);  
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);

  // Fade Out Green
  for (int x=0; x<256; x=x+5) {
    analogWrite (GreenPin, x);   
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);

  // Fade Out Blue
  for (int x=0; x<256; x=x+5) {
    analogWrite (BluePin, x);     
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);

  // Fade Out Cyan
  for (int x=0; x<256; x=x+5) {
    analogWrite (GreenPin, x);   
    analogWrite (BluePin, x);     
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);

  // Fade Out Yellow
  for (int x=0; x<256; x=x+5) {
    analogWrite (RedPin, x);  
    analogWrite (GreenPin, x);   
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);
  
  // Fade Out Magenta
  for (int x=0; x<256; x=x+5) {
    analogWrite (RedPin, x);  
    analogWrite (BluePin, x);     
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);

  // Fade Out White
  for (int x=0; x<256; x=x+5) {
    analogWrite (RedPin, (x / 2) + (256 - (256 / 2)));  
    analogWrite (GreenPin, x);   
    analogWrite (BluePin, x);     
    delay(10);   
  }
  // Clear All Colors
  analogWrite (RedPin, 255);  
  analogWrite (GreenPin, 255);   
  analogWrite (BluePin, 255);     
  delay(25);

}

Enjoy! :slight_smile:

Hi, Looks great..

May we use your schematic on the http://ArduinoInfo.Info WIKI??

The WIKI has this now: http://arduino-info.wikispaces.com/RGB-LED

I found that having the LED point up into a pingpong ball, or better a frosted plastic egg carton (or clear that you have sandpapered) makes the effect much more interesting.

Have fun!

Great stuff!

Now, here's a challenge for you- instead of the seven colors and fading them out individually-can you rewrite so that ALL colors the LED are capable of are shown?

HINT: Nesting.....

How about a really realistic flickering campfire :slight_smile:

May we use your schematic on the http://ArduinoInfo.Info WIKI??

I think the LED's are the wrong way round in the drawing. They are shown as common cathode and it's connected to 5V.

In the inimitable words of Miss Piggy, "I Knew That!"..

@clm3s, can you fix that for us all? Otherwise I'd have to FPS it (Fire Up Photoshop)...

WHOOPS! I should have known better! For shame on me...

I'll correct it as soon as I can. Thanks for the heads up!

XD

EDIT: I updated the image and it should be visible above. Feel free to use it.

Try this code and tell me what you think :slight_smile:

int Cred, Cgreen, Cblue, Nred, Ngreen, Nblue;

int RedPin = 10;
int GreenPin = 11;
int BluePin = 9;

int low=15;
int high=256;
int del=10;

void setup() 
{ 
    Serial.begin(9600);
    randomSeed(analogRead(0));
    Cred=random(low,high);
    Cgreen=random(low,high);
    Cblue=random(low,high);
    Nred=random(low,high);
    Ngreen=random(low,high);
    Nblue=random(low,high);
}
void loop() 
{ 
    while(Cred!=Nred)
    {
      if(Cred<Nred)
        Cred++;
      else
        Cred--;
     
      analogWrite (RedPin, Cred);
      delay(del);
    }
    
    while(Cgreen!=Ngreen)
    {
      if(Cgreen<Ngreen)
        Cgreen++;
      else
        Cgreen--;
      
      analogWrite (GreenPin, Cgreen);
      delay(del);
    }
    
    while(Cblue!=Nblue)
    {
      if(Cblue<Nblue)
        Cblue++;
      else
        Cblue--;
      
      analogWrite (BluePin, Cblue);
      delay(del);
    }
    
    Nred=random(low,high);
    Ngreen=random(low,high);
    Nblue=random(low,high);   
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.