An RGB Experiment

So.. Now that I have completed the XOR Simulation exercise and understood a bit about I/O on the Arduino board, I am all set to connect and light up an RGB LED. A simple setup it shall be. I will have three push buttons in pull down configuration (that means, when they are not pressed they are grounded — logical 0) and each corresponding to one of the RGB colour. So when the first button is pressed, it shall make the RGB LED glow Red, the second one shall make it glow blue and the third shall make it glow green. Also, if you were to press two of the buttons simultaneously, then both the corresponding colours should glow up(Hey, After all..its a RGB LED.. What fun without some colour combinations!).

Before we go into the circuit setup, lets understand the pin combinations of a 4 pin RGB LED. A normal 4 pin RGB LED’s come in two categories based on their pin combination. One being the common anode and the second being the common cathode. I guess in most of the cases, you will have with you a common cathode RGB LED and so let’s understand the pin markup of a common cathode RGB LED. It is as shown below :

RGB LED Markup

And here is the circuit setup :

Image

Below, is the code required to achieve the functionality as explained above

/* RGB Experiment - Blinking colors with RGB LED and three push
* buttons.

* Arduino shall detect the button press and shall make the RGB
* LED blink the corresponding colours. If two buttons are pressed
* together, then both the corresponding colours shall light up.
* But in case all three button are pressed together or all three
* are release, the RGB LED will be in off state.

created 31 Mar 2013
by Rajgopal Menon<rajgopalmenon.wordpress.com>
*/

//Declaring all constant values. These don't change
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int ledR = 9;
const int ledB = 10;
const int ledG = 11;

//Declaring all variable. These shall change during runtime
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup() {
//Setting the output to the RGB LED's
pinMode(ledR, OUTPUT);
pinMode(ledG, OUTPUT);
pinMode(ledB, OUTPUT);
//Setting the input ports through which button
//press is detected.
pinMode(buttonPin1,INPUT);
pinMode(buttonPin2,INPUT);
pinMode(buttonPin3,INPUT);

}

void loop() {
//Reads the button values
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);

//Executes the switching on and off of LED based on condtions.
//If two buttons are pressed, an analog write is done where the
//colours corresponding to the pressed buttons are give 2.49V each.
if(buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW){
digitalWrite(ledR, HIGH);
digitalWrite(ledB, LOW);
digitalWrite(ledG, LOW);
}
else if(buttonState2 == HIGH && buttonState1 == LOW && buttonState3 == LOW){
digitalWrite(ledG,LOW);
digitalWrite(ledR, LOW);
digitalWrite(ledB, HIGH);
}
else if(buttonState3 == HIGH && buttonState2== LOW && buttonState1 == LOW){
digitalWrite(ledB,LOW);
digitalWrite(ledR, LOW);
digitalWrite(ledG, HIGH);
}
else if(buttonState1 == HIGH && buttonState2 == HIGH && buttonState3 == LOW){
analogWrite(ledR,127);
analogWrite(ledB,127);
analogWrite(ledG,1);
}
else if(buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == HIGH){
analogWrite(ledR,127);
analogWrite(ledB,1);
analogWrite(ledG,127);
}
else if(buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == HIGH){
analogWrite(ledR,1);
analogWrite(ledB,127);
analogWrite(ledG,127);
}
else {
digitalWrite(ledB,LOW);
digitalWrite(ledR, LOW);
digitalWrite(ledG, LOW);
}

}

Hmm.. I see that my code has a big if-else-if snake. Currently I am unaware of a technique to write cleaner code for my sketches as I am just a beginner.  I will have to read more and learn more to make this code look better. But for now this is the way it is. I am open to views of anyone who has better experience in correcting this particular issue in the above code…or is it the way it is supposed to be?