Making The Windbond Sing!

Just wave you hand in the air!

Continue reading to see the code.

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

#define NUMREADINGS 25
#define DUPLICATES 2

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 duplicateReadings[DUPLICATES];
int duplicateIndex = 0;

int forwardPin = 9;                            // which light connected to digital pin 9
int recordPin = 10;
int playPin = 11;

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
  for (int i = 0; i < DUPLICATES; i++)
    duplicateReadings[i] = 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;

  // is the current reading the same as the last 2 readings?
  duplicateReadings[duplicateIndex] = closeValue;
  duplicateIndex = (duplicateIndex + 1);
  if (duplicateIndex >= DUPLICATES)
    duplicateIndex = 0;

if(duplicateReadings[duplicateIndex] == duplicateReadings[duplicateIndex – 1]) {
   if(duplicateReadings[duplicateIndex] < 75) {
      stop();
    } else if(duplicateReadings[duplicateIndex] >= 150 && duplicateReadings[duplicateIndex] <= 300) {
      play();
    } else if(duplicateReadings[duplicateIndex] > 400) {
      next();
    }
  }
}

void stop() {
    analogWrite(forwardPin, 255);
    analogWrite(recordPin, 255); 
    analogWrite(playPin, 255);
}

void play() {
    analogWrite(forwardPin, 255);
    analogWrite(recordPin, 255); 
    analogWrite(playPin, 0);
    delay(50);
    analogWrite(playPin, 255);
}

void next() {
    analogWrite(playPin, 255);
    analogWrite(forwardPin, 0);
    analogWrite(recordPin, 255);
    delay(50); 
    analogWrite(playPin, 0);
    analogWrite(forwardPin, 255);
    delay(500);
    analogWrite(playPin, 255);
    delay(3000);
    analogWrite(forwardPin, 0);
}