testSharpIRED_0.txt (5134Bytes)
Sharp IR sensor on Arduino with color LED feedack
I bought a Sharp GP2Y0A21YK0F Distance Sensor to use in my robot. It was very easy to see the data returned in a serial console window, but I thought it would be cool to see the distance in zones represented by colors from a tricolor LED. Then you would not need to be connected to the computer to tell distance. Green would indicate a safe distance, yellow would represent a cautionary distance and red would be for distances that would require my robot to take evasive action.
For this How-To, I have used the following equipment and pin outs:
* Arduino Diecimila
* Sharp GP2Y0A21YK0F Distance Sensor
Pin 1 => Analog 2
Pin 2 => Gnd
Pin 3 => +5V
* 1K 5% resistor (Brown Black Red Gold)
* T-1 3/4 5mm Full Color LED (Radio Shack part #276-028)
Pin 1 => Digital 9
Pin 2 => 1K resistor => +5V
Pin 3 => Digital 10
Pin 4 => Digital 11
Using the sensor is as simple as connecting the output to an analog pin and power to, well, power. All you have to do is read the pin value:
variable = analogRead(pin);
I noticed that the values tended to jump so I thought I could smooth the output by averaging the results. This seemed to work pretty well. It was easily done by defining a variable for number of results to average, looping through a function that many times while reading the analog data, adding them up and dividing by number of loops. The function returns the average:
//==========================================================================================
// Fuction to get distance from Sharp IRED unit
//==========================================================================================
int getDist()
{
for (i=0; i < avgFactor; i++)
{
distRead = analogRead(sharpIR);
runningTotal += distRead;
}
distAvg = (runningTotal / avgFactor);
return distAvg;
}
Once I have the average, I determine the appropriate color for the LED using a function:
//==========================================================================================
// Function to change LED color based on distance:
//==========================================================================================
int colorLEDs(int)
{
if (distAvg >= 500) //Turn on LED red if > 500
{
redLED();
}
else if ((distAvg >= 200) && (distAvg < 500)) //Turn on LED yellow-ish if 200 < dist > 500
{
yellowLED();
}
else if (distAvg < 200) //Turn on LED green if dist < 199
{
greenLED();
}
}
//==========================================================================================
// Light up LED Red
//==========================================================================================
int redLED() {
analogWrite(ledR, 0);
analogWrite(ledB, 255);
analogWrite(ledG, 255);
}
//==========================================================================================
// Light up LED Yellow
//==========================================================================================
int yellowLED() {
analogWrite(ledR, 70);
analogWrite(ledB, 240);
analogWrite(ledG, 10);
}
//==========================================================================================
// Light up LED Green
//==========================================================================================
int greenLED() {
analogWrite(ledR, 255);
analogWrite(ledB, 255);
analogWrite(ledG, 0);
}
//==========================================================================================
// Turn off LED
//==========================================================================================
int offLED() {
analogWrite(ledR, 255);
analogWrite(ledG, 255);
analogWrite(ledB, 255);
}
The above values work out to about: < 3.5" = red, 3.5" - 8" = yellow, and +8" = green. Of course, you can change these to whatever you want.
Full code is attached. Feel free to use, modify, comment, ridicule or ignore. I hope this proves to be useful to someone. I don't know where my video went...
https://www.youtube.com/watch?v=xY3Uq_-Icok