IR sensor + 3 LEDS

Here’s the fine video of my IR sensor actually working!!

http://vimeo.com/moogaloop.swf?clip_id=2093728&server=vimeo.com&show_title=1&show_byline=0&show_portrait=0&color=00ADEF&fullscreen=1
Another IR sensor + 3 LEDS from Joana Kelly on Vimeo.

I had some wacky issues connecting my IR sensor, but found a useful diagram, also including here:


I ended up adapting Matt’s code also. My original code made the lights flicker a lot, but Matt averaged out the IR signal quite effectively. Kudos to you, sir. Matt’s code is also after the jump.

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);
  }

}