Pages

12 June 2011

Arduino UNO: LED Sensor, Part Two

Building on the last project, I am now using a Red and a Yellow LED as a Sensor to detect light coming from an RGB LED.

Putting different coloured Mega Bloks over the LEDs has different effects on the Sensors as the RGB LED gets brighter and brighter.

I used the Processing Language to control the brightness of the RGB LED through a Serial command, and then use the resulting Sensor readings from the Yellow and the Red LEDs to create a chart or plot.

Here are the results of my experiment.

Red Mega Blok

Yellow Mega Blok

Green Mega Blok

When the displayed bars are RED, it indicates that the Red LED is absorbing MORE light than the Yellow LED (and vice versa). Hence this is a "Difference Chart".
The Green Mega Blok absorbs more Red Light than the other blocks, therefore producing a big difference between Red LED sensor readings and Yellow Sensor readings.

/* Processing code for this example */// Graphing sketch
//This sketch was written by ScottC, but was adapted from a sketch
//written by Tom Igoe in 2005
// This example code is in the public domain.
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float YellowVal=0; // The variable to hold the Yellow Sensor Reading
float RedVal=0; // The variable to hold the Red Sensor Reading
float Diff=0; // The variable to hold the difference between the readings
int Switcher=0; // Used to control the flow of the program
voidsetup () {
// set the window size:
size(1020, 750);
// List all the available serial ports
println(Serial.list());
// I use COM13 for my Serial Port - you will need to change this to suit your system
myPort = new Serial(this, "COM13", 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\\n');
// set inital background:
background(0);
//Send a value to the Arduino to start the feedback mechanism
myPort.write(0);
}
voiddraw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
//The arduino sends 2 sensor readings. The following code
//helps to identify which reading is which.
if(inString.equals("Y")){
Switcher=0;
} elseif (inString.equals("R")){
Switcher=1;
} else {
//Convert the String to a float
float inByte = float(inString);
//Map the reading, so that the chart fits within the window.
inByte = map(inByte, 0, 1000, 0, height);
if(Switcher==0){
//Save the reading from the yellow sensor to YellowVal
YellowVal=inByte;
} else {
//Save the reading from the red sensor to RedVal
RedVal=inByte;
//Calculate the difference between the readings
Diff=RedVal-YellowVal;
//If the yellow sensor is greater, plot with a yellow line
//If the red sensor reading is greater, plot a red line.
if(Diff<=0){
stroke(255,255,0);
Diff=abs(Diff);
} else {
stroke(255,0,0);
}
// draw the line:
line(xPos, height, xPos, height - Diff);
// at the edge of the screen, go back to the beginning:
if (xPos > width) {
xPos = 0;
background(0);
//Send a value to the Arduino to change the intensity
//of the RGB LED and take another reading
myPort.write(xPos);
} else {
// increment the horizontal position: Increment by more
// to get less readings and to make it quicker
xPos+=4;
if (xPos>0){
//Send a value to the Arduino to change the intensity
//of the RGB LED and take another reading
myPort.write(xPos/4);
} else {
myPort.write(xPos);
}
}
}
}
}
}