Pages: [1]   Go Down
Author Topic: RGB to White  (Read 734 times)
0 Members and 1 Guest are viewing this topic.
Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know this is very basic, but I'm just started at getting back into Arduino. The main reason I'm posting this is so that the people who watch my video can see the code. Let me know what you think of this.

About the project: I am making some custom firniture for my room. I want to add some lighting to it. It is gonna be controlled by some pots, switches, ect. However, when you try to make white light with an RGB led, it is very impure. So what I'm gonna do is add pure white LEDs next to the RGB. This is how I plan to have it switch between the RGB, and the White LED.

Code:
int red = 11;    // Red lead of RGB attached to pin 11
int green = 10;  // Green lead of RGB attached to pin 10
int blue = 9;    // Blue lead of RGB attached to pin 9
int white = 8;   // White LED attached to pin 8

int redPot = 0;    // Pot for controlling red value attached to analog pin 0
int greenPot = 1;  // Pot for controlling green value attached to analog pin 1
int bluePot = 2;   // Pot for controlling blue value attached to analog pin 2

int redVal;    // Variable for storing the red value
int greenVal;  // Variable for storing the green value
int blueVal;   // Variable for storing the blue value

void setup() {               
 pinMode(red, OUTPUT);    // Red pin is an output
 pinMode(green, OUTPUT);  // Green pin is an output
 pinMode(blue, OUTPUT);   // Blue pin is an output
 pinMode(white, OUTPUT);  // White pin is an output
}

void loop() {
 redVal = analogRead(redPot);      // Reads the value of the Red Pot, and stores the value in Red Val
 greenVal = analogRead(greenPot);  // Reads the value of the Green Pot, and stores the value in Green Val
 blueVal = analogRead(bluePot);    // Reads the value of the Blue Pot, and stores the value in Blue Val
 
 redVal = map(redVal, 0, 1023, 0, 255);      // Maps Red Val to fit the perameters of PWM
 greenVal = map(greenVal, 0, 1023, 0, 255);  // Maps Green Val to fit the perameters of PWM
 blueVal = map(blueVal, 0, 1023, 0, 255);    // Maps Blue Val to fit the perameters of PWM
 
 if(redVal >= 250 && greenVal >= 250 && blueVal >= 250){  // If Red Val, Green Val, and Blue Val are all equal to or above 250
  digitalWrite(red, LOW);                                 // Turn the Red Pin off
  digitalWrite(green, LOW);                               // Turn the Green Pin off
  digitalWrite(blue, LOW);                                // Turn the Blue Pin off
  digitalWrite(white, HIGH);                              // Turn the White Pin on
 }
 else{
  analogWrite(red, redVal);      // PWM the Red Pin to the value of Red Val
  analogWrite(green, greenVal);  // PWM the Green Pin to the value of Green Val
  analogWrite(blue, blueVal);    // PWM the Blue Pin to the value of Blue Val
  digitalWrite(white, LOW);      // Turn the White Pin off
 } 
}
Logged

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

You really shouldn't be driving LED's directly from pins if they are high-brightness types.. you may want to use a switching transistor so that you can provide more current and potentially add more LED's...

Also, I've found that the "whiteness" can be helped by PWM blending of white with the RGB.. allowing for more light output.  I typically put all the LED's (RGB+W) inside a semi-translucent shell, which helps blend the colors.  Ping Pong balls work well for this...
Logged

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

Somewhere in Arizona
Offline Offline
God Member
*****
Karma: 0
Posts: 725
Arduino must be a drug, because I'm addicted!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You really shouldn't be driving LED's directly from pins if they are high-brightness types.. you may want to use a switching transistor so that you can provide more current and potentially add more LED's...
They have 1k resistors, so they don't draw much power. When I use my LED strips, I will definately use some big beefy transistors. But for now, it's fine.

Also, I've found that the "whiteness" can be helped by PWM blending of white with the RGB.. allowing for more light output.  I typically put all the LED's (RGB+W) inside a semi-translucent shell, which helps blend the colors.  Ping Pong balls work well for this...
Well, I would just prefer to turn the RGB off completely, as to save some power. After all, when I do upgrade to strips, having both the RGB, and the White strip on full blast would require quite a big power supply.
Logged

Pages: [1]   Go Up
Jump to: