3 LED IR Setup

http://vimeo.com/moogaloop.swf?clip_id=2091681&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1
3 LED IR Setup from Mouse & the Billionaire on Vimeo.

I had some problems earlier with light flickering, but I solved this by averaging the input from the IR sensor and smoothing out the results. Works great now.

Code

// Code to control 3 LEDs based on hand placement in relationship to an IR sensor
// M Bethancourt
// 2008

#define NUMREADINGS 10

int readings[NUMREADINGS];                // the readings from the analog input
int index = 0;                            // the index of the current reading
int total = 0;                            // the running total
int average = 0;                          // the average

int rLed = 9;                            // which light connected to digital pin 9
int gLed = 10;
int bLed = 11;

int rValue = 255;                           // variable to keep the actual value
int gValue = 255;                           // variable to keep the actual value
int bValue = 255;                           // variable to keep the actual value

int rangeFinder = 5;
int closeValue = 0;

void setup()
{
  Serial.begin(9600);                       //  setup serial
  for (int i = 0; i < NUMREADINGS; i++)
    readings[i] = 0;                        // initialize all the readings to 0
}

void loop() {
  total -= readings[index];               // subtract the last reading
  readings[index] = analogRead(rangeFinder); // read from the sensor
  total += readings[index];               // add the reading to the total
  index = (index + 1);                    // advance to the next index

  if (index >= NUMREADINGS)               // if we’re at the end of the array…
    index = 0;                            // …wrap around to the beginning

  average = total / NUMREADINGS;          // calculate the average
  Serial.println(average);                // send it to the computer (as ASCII digits)
  closeValue = average;


   

  if(closeValue < 75){
    analogWrite(rLed, 255);
    analogWrite(gLed, 0); 
    analogWrite(bLed, 0);
  } else if(closeValue >= 75 && closeValue <= 500){
    analogWrite(rLed, 0);
    analogWrite(gLed, 255); 
    analogWrite(bLed, 0);
  } else if(closeValue > 500) {
    analogWrite(rLed, 0);
    analogWrite(gLed, 0); 
    analogWrite(bLed, 255);
  }

}