InduinoX User Guide - Programming the RGB LED

In this edition we will see how to use the RGB LED on the InduinoX Board.  The RGB LED was included as part of the InduinoX board for the user to be able to experiment with analog outputs / PWM.

The InduinoX board has 6 PWM Pins [Pins 3,5,6,9,10,11] that can generate a PWM signal of 8-bit resolution.[8-bits can represent a maximum value of 255, and a 8-bit resolution here means that 5 volts is represented by 255 divisions. So if you want to generate 1 volt, you would use the value 51]

The RGB LED is connected to PWM Pins 3,5 & 6. [Blue to 3, Red to 5 & Green to 6] The RGB LED is a common Cathode Type LED
.
The Intensity of each of the Color Spectrum [Red, Green & Blue] can be varied by controlling the voltage applied on the individual pins. The PWM pins in addition to generating digital HIGH / LOW signals can generate analog voltages between 0 & 5. The analogWrite function will help us use these pins.

The analogWrite function will take a 8-bit numerical value as a parameter [called duty cycle] and produce an output voltage corresponding to this value. It will set the pin to generate a steady square wave of the specified duty cycle at roughly 490Hz frequency.

So Lets try to experiment with the RGB LED. Try the following code
 void setup()  
 {  
 }  
 void loop()  
 {  
  analogWrite(3,153);// Setting the voltage for Blue to around 3 Volts  
  analogWrite(5,51);// Setting the voltage for Red to around 1 Volt  
  analogWrite(6,51);// Setting the voltage for Green to around 1 Volt  
 }  

Now you will see that the RGB LED glows with more of Blue color. Try to change the Values for the other Colors & experiment.

Here's a small project implementing the LEDs, Buttons & the RGB LED.

2 Buttons - 7 & 9 are used to increment and decrement the brightness of a chosen color in the RGB LED. A color can be chosen by using the Button on the 8th pin. The 3 regular LEDs are used to display the chosen color. When you choose Blue, the Blue LED will glow. In addition to this, pressing the Buttons 7 & 9 at the same time will cause the RGB LED to be reset to its initial state - all colors become zero. Note that the increment and decrement happen in a cyclic fashion ( 255 ++ will become 0 and 0 -- will become 255) Enjoy!

#### Video Errata - The RGB LED is a Common Cathode Type & Not Common Anode as mentioned in the Video####





Here's the Source Code

 /*  
 ***********************************************************************************************************************  
 *******************************### RGB LED COLOR CONTROL USING BUTTONS ###*********************************************  
 */  
 int RGB[3]; // Variable to store individual Color Value of Different Colors of the RGB LED  
 int arr_ind=0; // Variable for Navigating through the Above Array - An Aray Index Variable  
 void setup()  
 {  
  pinMode(7,INPUT); // The Increment Button  
  pinMode(8,INPUT); // The Color Select Button  
  pinMode(9,INPUT); // The Decrement Button  
  digitalWrite(7,HIGH); // Enabling the Internal Pull-Up Resistor for the Button  
  digitalWrite(8,HIGH);// Enabling the Internal Pull-Up Resistor for the Button  
  digitalWrite(9,HIGH);// Enabling the Internal Pull-Up Resistor for the Button  
  pinMode(11,OUTPUT);//LED for Current Selected Color Indication for RED Color - Lights up When the User Selects RED Color  
  pinMode(12,OUTPUT);//LED for Current Selected Color Indication for BLUE Color - Lights up When the User Selects BLUE Color  
  pinMode(13,OUTPUT);//LED for Current Selected Color Indication for GREEN Color - Lights up When the User Selects GREEN Color  
  RGB[0] = 0; // RGB[0] will store the value for the BLUE Color  
  RGB[1] = 0; // RGB[1] will store the value for the RED Color  
  RGB[2] = 0; // RGB[2] will store the value for the GREEN Color  
  applyColor(); // Calling a Function that will handle the AnalogWrite functions for the RGB LED  
 }  
 void loop()  
 {  
  if(digitalRead(7)==0) // Checking if the Increment button is Being Pressed, If True, the value of the currently selected color's value is incremented  
  {  
   if(RGB[arr_ind]<255) // Checks if the currently selected color value is lesser than 255 before incrementing. So when it reaches 255, the value is reset to 0.  
    RGB[arr_ind]++;  
   else  
    RGB[arr_ind]=0;  
   delay(100);  
  }  
  if(digitalRead(9)==0)// Checking if the Decrement button is Being Pressed, If True, the value of the currently selected color's value is decremented  
  {  
   if(RGB[arr_ind]>0)// Checks if the currently selected color value is greater than 0 before decrementing. So when it reaches 0, the value is reset to 255.  
    RGB[arr_ind]--;  
   else  
    RGB[arr_ind]=255;  
   delay(100);  
  }  
  if(digitalRead(8)==0)// Checking if the color button is Being Pressed, If True, the value of the array index is incremented to the next value  
  {  
   if(arr_ind<2)  
    arr_ind++;  
   else  
    arr_ind=0;  
   while(digitalRead(8)==0); // This while is used to debounce the button press or in other words, wait for the user to release the button  
   delay(50);  
  }  
  if((digitalRead(7)==0)&&(digitalRead(9)==0))// Checking if both the increment & decrement buttons are being pressed at the same time. If so, all color values are reset to zero  
  {  
   RGB[0] = 0; // RGB[0] will store the value for the BLUE Color  
   RGB[1] = 0; // RGB[1] will store the value for the RED Color  
   RGB[2] = 0; // RGB[2] will store the value for the GREEN Color  
   digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,HIGH);// This to indicate a reset in progress. All three LEDS GLOW for 200 milliseconds and go Off  
   delay(200);  
   digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,LOW);  
  }  
  switch(arr_ind) // The switch is used to indicate the current color selection through the corresponding LED based on the current value of the Array Index  
  {  
   case 0: digitalWrite(11,LOW);digitalWrite(12,HIGH);digitalWrite(13,LOW);break;  
   case 1: digitalWrite(11,HIGH);digitalWrite(12,LOW);digitalWrite(13,LOW);break;  
   case 2: digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,HIGH);break;  
  }  
  applyColor();// Calling a Function that will handle the AnalogWrite functions for the RGB LED  
 }  
 // The function applyColor() will apply the RGB array variable's current value to the Analog Pins 3,5 & 6 which control the RGB LED  
 void applyColor()  
 {  
  analogWrite(3,RGB[0]);  
  analogWrite(5,RGB[1]);  
  analogWrite(6,RGB[2]);  
 }