Pages: [1]   Go Down
Author Topic: My First Sketch: PWM Common Annode RGB Led 7 Color Generator  (Read 2463 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
I gave up electronics for programing, now they tell me I can program electronics...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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!  smiley
« Last Edit: January 17, 2013, 01:02:29 am by clm3s » Logged

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1926
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
 
Logged

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

Boston Suburbs
Offline Offline
God Member
*****
Karma: 16
Posts: 955
I am above your silly so-called "Laws", Mister Ohm.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.....
Logged

When the testing is complete there will be... cake.

Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1926
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How about a really realistic flickering campfire  smiley
Logged

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

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 70
Posts: 2575
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Topsham, Vermont USA
Offline Offline
Edison Member
*
Karma: 33
Posts: 1926
... in The Woods In Vermont
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)...
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
I gave up electronics for programing, now they tell me I can program electronics...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

 smiley-lol

EDIT: I updated the image and it should be visible above. Feel free to use it.
« Last Edit: January 17, 2013, 01:05:04 am by clm3s » Logged

Maine, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this code and tell me what you think smiley

Code:
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.
« Last Edit: February 12, 2013, 02:08:44 am by Coding Badly » Logged

Pages: [1]   Go Up
Jump to: