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?

XOR Simulation using Arduino

In this post, an experiment with Arduino to create a setup which simulates the XOR truth tables using two tactile buttons and an LED is explained.  The circuit setup is as below. The diagram is created using Fritzing – An EDA tool (Check it out!)

Image

Setting up the Arduino IDE in GNU/Linux :

Dowload the latest version of the Arduino IDE.

After downloading, extract the archive and the execute the arduino script to open the IDE.

After opening the IDE, set the board and port used in the Tools menu. Goto Tools-> Board -> Arduino UNO (or any board which you use). Then again goto Tools -> Serial Port -> /dev/ttyACM0 (or any other port to which the arduino is connected.) To check which port the arduino is using, you can disconnect and reconnect the Arduino USB and check kernel logs using ‘dmesg’ command. For Arduino UNO (which is an ACM device) you will mostly see the following line in dmesg :

[ 1956.390390] cdc_acm 2-1.6:1.0: ttyACM0: USB ACM device (The port is mentioned in the dmesg line)

If you get such a line the port is, as mentioned above, /dev/ttyACM0.

After setting the IDE, you can start a new Sketch and use the below code to setup the Arduino.

/*
 Xor Buttons

 Turns on and off a light emitting diode(LED) connected to digital
 pin 13, when pressing a pushbuttons attached to pin 2 and pin 4 based
 on the XOR truth table

 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * pushbutton attached to pin 4 from +5V
 * 10K resistor attached to pin 2 from ground
 * 10K resistor attached to pin 4 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.

 created 2005
 by DojoDave
 modified 30 Aug 2011
 by Tom Igoe
 modified from original "Button" code on 30 Mar 2013
 By Rajgopal

 The original Button code is an example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/Button

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2;     // the number of the pushbutton1 pin
const int buttonPin2 = 4;    // the number of the pushbutton2 pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState1 = 0;         // variables for reading the pushbutton status
int buttonState2 = 0;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbuttons pin as an inputs:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop(){
  // read the state of the pushbuttons value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  // check if the pushbuttons is pressed.
  // Based on the pushbutton status and truth table of XOR, turn on or off the LED at Pin 13
  if ((buttonState1 == HIGH && buttonState2 == LOW) || (buttonState1 == LOW && buttonState2 == HIGH)) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

First you should “Verify and Compile” the code. Goto Sketch->Verify/Compile. If the compilation and verification is successful, then you can go ahead and upload your sketch’s binary to the Arduino micro-controller. Goto File -> Upload. If the upload is successful, then you can test the setup. Press either one of the tactile button and the LED should glow. If both tactile buttons are pressed together or if both are released together, then the LED shouldn’t glow, thus perfectly simulating XOR gate.

My Arduino UNO R3

Finally! The days of Arduino Hacking is here. I bought my own Arduino UNO from Simplelabs along with the Quick Start kit – the main attraction of which was the solder-less breadboard, the wire-stripper and the screwdriver.. (yeah..like I needed those desperately!).

My Ardiuno UNO

My Arduino UNO R3

Now that I got the Arduino, I wasted no time in pleasing the embedded gods by uploading the classical “hello world” program of the bit world..lovingly called the “Blink”.

The “Blink” is one of the pre-defined example in the Arduino IDE and you just have to verify and compile it and then connect your Arduino board and upload the compiled program image onto the microcontroller. (Yes. there are some pre-requisites before for setting up the IDE and its pretty straightforward. I will write about this setup in the next post.) On successful upload you can usually see the on-board LED named ‘L’ starting to blink. If you are still not convinced with the on-board LED,  you can check the same output by connecting an actual LED between the Port 13 and GND (with Anode[+], which is the longer leg of LED, connected to I/O port 13 and cathode connected to the GND port).

And it shall blink!

The LED in OFF State

LED Turned OFF – Arduino Blink Working

LED turned on

LED Turned ON – Arduino Blink Working