Sunday, May 23, 2010

RGB LED Fun


Here's the first thing I programmed up with my new Arduino! (I'm not new to Atmel μcontrollers but new to the Arduino) It's just a diffused RGB LED that goes red-green-blue-yellow-purple-turquoiseish (last one was supposed to be white, got tired of fiddling with it). Figured I'd post the code since Sparkfun didn't have a link for an Arduino example, just the Wiring example I took and modified. All that really needs to change from the example is the PWM pins since they are different on the Arduino.

int RED = 3;    // RED pin of the LED to PWM pin 3
int GREEN = 6;  // GREEN pin of the LED to PWM pin 6
int BLUE = 5;   // BLUE pin of the LED to PWM pin 5
int gw = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  for(int r = 0; r < 64; r+=1) {
    analogWrite(BLUE, 0);
    analogWrite(GREEN, 0);
    analogWrite(RED, r);
    delay(60);
  }
  for(int g = 0; g < 64; g+=1) {
    analogWrite(BLUE, 0);
    analogWrite(RED, 0);
    analogWrite(GREEN, g);
    delay(60);
  }
  for(int b = 0; b < 256; b+=4) {    
    analogWrite(RED, 0);
    analogWrite(GREEN, 0);
    analogWrite(BLUE, b);
    delay(60);
       /* Code if you want to "see" what's going on the Serial Monitor, useful for debugging
        Serial.print(r);
        Serial.print("\t");
        Serial.print(g);
        Serial.print("\t");
        Serial.print(b);
        Serial.println();*/
     
   
  }
  for(int y = 0; y < 64; y+=1) {
    analogWrite(BLUE, 0);
    analogWrite(RED, y);
    analogWrite(GREEN, y);
    delay(60);
  }
    for(int p = 0; p < 64; p+=1) {
    analogWrite(BLUE, p);
    analogWrite(RED, p);
    analogWrite(GREEN, 0);
    delay(60);
  }
    for(int w = 0; w < 64; w+=1) {
    analogWrite(BLUE, w);
    analogWrite(RED, 0);
    gw=w;
    if(gw != 0)
      gw+=5;
    analogWrite(GREEN, gw);
    delay(60);
  }
}



Next I'll be posting about the project that I actually got all this hardware for; note the Kent Display in the pic :)

2 comments:

Amie McCracken said...

Where's the note about my awesome banner making?

A1 said...

What banner? All I see is a header ;)