How May I Help You?: Code

The code is very simple.  Take a look by clicking the following link.

/*Fuki
Final Project: How May I Help You?
Wireless Toys
*/

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

// INPUT: IR Sensor should be connected to 5V and GND
int irPin = 3; // IR Sensor output connected to analog pin 3
int irVal = 0; // Variable to store the input from the IR Sensor

// OUTPUT: Use digital pins 9 & 10, the Pulse-width Modulation (PWM) pins
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()
{
   irVal = analogRead(irPin);   // read the IR sensor value at the input pin
   average[counter] = irVal;

  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 > 10)               // Print every hundred loops
    {
      DEBUG = 1;                   // Reset the counter
                                   // Serial output using 0004-style functions
      Serial.print("irVal:");    // Indicate that output is play value
      Serial.print(irVal);       // 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 (averaged <= 10) {  // Middle of IR sensor range
      playVal = LOW;  // play
      //delay(300);
      fwdVal = HIGH; 
      delay(300);
    }
      else if (averaged >= 10) { // Middle third of IR sensor range
      playVal = HIGH;
      //delay(300);
      // play
      //fwdVal = HIGH;
      delay(300);
    }
      else  // Catch anything else
    {
      playVal = HIGH;
     //delay(300);
      fwdVal = HIGH; 
     delay(300);      
    }

    //delay(300);
    digitalWrite(playPin, playVal);   // Write values to the output pins
    //delay(300);
    digitalWrite(fwdPin, fwdVal);
  }