Go Down

Topic: My First Sketch: PWM Common Annode RGB Led 7 Color Generator (Read 2 times) previous topic - next topic

clm3s

Jan 16, 2013, 06:09 am Last Edit: Jan 17, 2013, 07:02 am by clm3s Reason: 1
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!

Code: [Select]

// 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!  :)

terryking228

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!
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

focalist

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.....
When the testing is complete there will be... cake.

terryking228

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Riva

Quote
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.
http://forum.arduino.cc/index.php?action=unread;boards=5,67,10,11,66,12,15,17,21,22,23,24,25,29;ALL

terryking228

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)...
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

clm3s

#6
Jan 17, 2013, 06:21 am Last Edit: Jan 17, 2013, 07:05 am by clm3s Reason: 1
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.

oniudranube

#7
Feb 12, 2013, 02:44 am Last Edit: Feb 12, 2013, 08:08 am by Coding Badly Reason: 1
Try this code and tell me what you think :)

Code: [Select]
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: [code] [/code] tags added.

Go Up