RGB LED

From STEAMwiki
Jump to: navigation, search

The code was taken from Adafruit

Sample Code:

/* Adafruit Arduino - Lesson 3. RGB LED */

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

//uncomment this line if using a Common Anode LED
#define COMMON_ANODE

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop()
{
  setColor(255, 0, 0);  // red
  delay(800);
  setColor(0, 255, 0);  // green
  delay(800);
  setColor(0, 0, 255);  // blue
  delay(800);
  
}

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
#endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}