Controlling RGB LEDs

RGB LEDs have a common cathode (or anode) as a long pin. There are three more pins for each color Red, Blue and Green.

Sketch

For both analog and digital control

#define RED_PIN 3
#define GREEN_PIN 6
#define BLUE_PIN 5

void setup() {
  // put your setup code here, to run once:
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  //RGB_digital();
  RGB_analog();


}

void RGB_ditial(){
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(RED_PIN, HIGH);
  delay(1000);
  digitalWrite(RED_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);
  digitalWrite(BLUE_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  delay(1000);
}

void RGB_analog(){
  analogWrite(GREEN_PIN, 50);
  analogWrite(RED_PIN, 100);
  analogWrite(BLUE_PIN, 100);
}

Using external libraries

1) Go to Sketch -> Include library -> Manage Libraries

2) Install ALA library. The library can be found under the sketch directory defined in the preferences

3) Check usage by browsing Examples under File -> Examples -> ALA

Here is a sketch for blending RGB colors .Browse through documentation for this library for more details about usage and parameters

NOTE : Be aware of the size used by these libraries. The following few lines of code occupy 20KB (almost 65% of SRAM memory)

#include <AlaLedRgb.h>

#define REDPIN 11
#define GREENPIN 9
#define BLUEPIN 10

AlaLedRgb rgbLed;

void setup()
{
  rgbLed.initPWM(REDPIN, GREENPIN, BLUEPIN);                 // initialize output pins
  rgbLed.setBrightness(0x66FF44);                            // calibrate white
  rgbLed.setAnimation(ALA_FADECOLORSLOOP, 5000, alaPalRgb);  // set the animation
}

void loop()
{
  rgbLed.runAnimation();  // run the animation indefinitely
}