IRSensor + Winbond Chip

Okay.. it was kinda working… 

The code that I modified is below:

/* Parts of the code also taken from:
* Analog Average – Rolling Buffer
* Jeff Gray – 2008
* ——————
* Averaging Code, modified from Dave Millis example
*/

// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3
int potVal = 0; // Variable to store the input from the potentiometer

// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED’s cathodes should be connected to digital GND
int playPin = 9;   // Play,   connected to digital pin 9
int fwdPin = 10;  // Forward, connected to digital pin 10

// Program variables
int playVal = 0;   // Variables to store the values to send to the pins
int fwdVal = 0;

int average[100];  //an array to calculate the average
byte counter = 0;
int averaged;

int DEBUG = 1;          // Set to 1 to turn on debugging output

void setup()
{
  pinMode(playPin, OUTPUT);   // sets the pins as output
  pinMode(fwdPin, OUTPUT);   

  if (DEBUG) {           // If we want to see the pin values for debugging…
    Serial.begin(9600);  // …set up the serial ouput in 0004 format
  }
}

// Main program
void loop()
{
   potVal = analogRead(potPin);   // read the potentiometer value at the input pin
   average[counter] = potVal;

  byte c;
  int total = 0;

  for(c = 0; c < 100; c++){
    total += average[c];
  } 

   averaged = total / 100;

  if(counter = 100){
    contrlPlay();
  }

  counter = (counter + 1) % 100;

  if (DEBUG) {                     // If we want to read the output
    DEBUG += 1;                    // Increment the DEBUG counter
    if (DEBUG > 100)               // Print every hundred loops
    {
      DEBUG = 1;                   // Reset the counter
                                   // Serial output using 0004-style functions
      Serial.print("potVal:");    // Indicate that output is play value
      Serial.print(potVal);       // Print play value
      Serial.print("\t");          // Print a tab
      Serial.print("averaged:");          // Repeat for grn and blu…
      Serial.print(averaged);
      Serial.print("\n");   

    }
  }
}

void contrlPlay(){
    if (potVal < 341) {  // Lowest third of the potentiometer’s range (0-340)

     // potVal = (averaged * 3) / 4; // Normalize to 0-255
      playVal = HIGH;  // play
      fwdVal = LOW;   
    }
      else if (potVal < 682) { // Middle third of potentiometer’s range (341-681)

     // potVal = ( (averaged-341) * 3) / 4); // Normalize to 0-255
      playVal = LOW;            // play
      fwdVal = HIGH;
    }
      else  // Upper third of potentiometer"s range (682-1023)
    {
      //potVal = ( (averaged-683) )* 3) / 4; // Normalize to 0-255
      playVal = LOW;
      fwdVal = LOW;         
    }
    digitalWrite(playPin, playVal);   // Write values to the output pins
    digitalWrite(fwdPin, fwdVal);
  }