Saturday, February 9, 2013

RGB Light Dimmer

Made a light dimmer that will control a multicolored LED through the colors of the rainbow.  I started with concept from this post and expanded it. The code can be seen after the jump break.




The code was a little confusing to setup because I used PWMs as grounds so that I could vary the light going to the RGB LED. This meant that values ranged from 255 which was off, and 0 which was on instead of the opposite which is typical. Also, like the project linked above, the range of the potentiometer is not comparable to the LEDs range of value. Instead of scaling to 255, I scaled to three times 255, 765, to account for the full range of values for three LEDs. 

int analogPin = 3;     
int ledB= 5;
int ledG= 6;
int ledR= 9;
int val = 0;           
int ledPin= 6;
void setup()
{
  Serial.begin(9600);          
}

void loop()
{
  val = analogRead(analogPin)/1.365;   
  if(val>0 && val<255)
  {
    int redRange= 255-val;
    analogWrite(ledR,redRange);
    analogWrite(ledG,255);
    analogWrite(ledB,255);
  }  
  if(val>=255 && val<510)
  {
    int greenRange=255-val-255;
    analogWrite(ledG,greenRange);
    analogWrite(ledR,255-greenRange);
    analogWrite(ledB,255);
  }
  if(val>=510)
  {
    int blueRange = 255-val-510;
    analogWrite(ledB,blueRange);
    analogWrite(ledG,255-blueRange);
    analogWrite(ledR,255);
  }
}

No comments:

Post a Comment