Saturday, April 26, 2014

Arduino Basics 3: RGB LED

Objective:
Control the colours of an RGB LED.

Parts:
RGB LED
330 Ohm Resistor



Sketch:
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

void setup()
{
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop()
{
  mainColors();
  showSpectrum();
}

void mainColors()
{

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);

  delay(1000);

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);

  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);

  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);
}

void showSpectrum()
{
  int x;
  for (x = 0; x < 768; x++)
    {
      showRGB(x);
      delay(10);
    }
}


void showRGB(int color)
{
  int redIntensity;
  int greenIntensity;
  int blueIntensity;

  if (color <= 255)
  {
    redIntensity = 255 - color;
    greenIntensity = color;       
    blueIntensity = 0;             
  }

  else if (color <= 511)
  {
    redIntensity = 0;
    greenIntensity = 255 - (color - 256);
    blueIntensity = (color - 256);
  }

  else
  {
    redIntensity = (color - 512);
    greenIntensity = 0;
    blueIntensity = 255 - (color - 512);
  }

  analogWrite(RED_PIN, redIntensity);
  analogWrite(BLUE_PIN, blueIntensity);
  analogWrite(GREEN_PIN, greenIntensity);

}

Analysis:
The Red pin [9], Green pin [10], and the Blue pin [11] were controlled by the functions "mainColors" and "showSpectrum".

The function mainColors turns the colour Red, Green, Blue, Yellow, Cyan, Purple, and White on the RGB LED one by one. Red, Green and Blue are the main RGB LED's colour. The other colours are turned on by turning on some or all of the main colours simultaneously. Yellow is turned on by Red and Green, Cyan is turned on by Green and Blue, Purple is turned on by Red and Blue, and White is turned on by all Red, Green, and Blue.

The function showSpectrum shows all of those colours in a fading form. This is done by using another function,"showRGB" inside showSpectrum. Variable "x" is used to represent a colour for the function showRGB.

If variable "x" is less than or equal to 255, the colour will be around Red. If variable "x" is less than or equal to 511, the colour will be around Green. If variable "x" is more than 511, the colour will be around Blue.

No comments:

Post a Comment