This is my documentation of the arduino clapper project that I did in June 2011. I apologize for the poor code readability, I was trying to make something work as quickly as possible.
The code below is old - Click for updated code: 2/2/13
If you have any doubts or something about the code/schematic isn't clear, just comment and I'll try to explain. Once the concept/code flow becomes clear, it's easy to code.
/** * Clapper project* author: Manoj Kunthu* last update: 6/5/11*/
int clap = 1;boolean enabled = true;int sensorValue = 0;
int SIZE = 25;int buffer[25];
int now = 0;int average = 0;int total = 0;int offset = 30;boolean initialized = false;int out = 10;
const int inPin0 = A0; const int clapLed1 = 12;const int clapLed2 = 11;
const int buttonPin = 2; // the pin that the pushbutton is attached toconst int ledPin = 3; // the pin that the LED is attached to
// Variables will change:int buttonPushCounter = 0; // counter for the number of button pressesint buttonState = 0; // current state of the buttonint lastButtonState = 0; // previous state of the button
void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(out, OUTPUT); // initialize the button pin as a input: pinMode(buttonPin, INPUT); // initialize the LED as an output: pinMode(ledPin, OUTPUT); pinMode(clapLed1, OUTPUT); pinMode(clapLed2, OUTPUT);
Serial.begin(9600);}
void loop() { // read the pushbutton input pin: buttonState = digitalRead(buttonPin); // compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { buttonPushCounter++; } } // save the current state as the last state, //for next time through the loop lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) { digitalWrite(ledPin, HIGH); enabled = true; } else { digitalWrite(ledPin, LOW); digitalWrite(clapLed1, LOW); digitalWrite(clapLed2, LOW); enabled = false; }
if(enabled){ sensorValue = analogRead(inPin0);
//insert the sensor value into the array if(initialized){ total = total - buffer[now] + sensorValue; } else{ total = total + sensorValue;
if(now + 1 >= SIZE) initialized = true; }
//Find the average room noise level if(initialized) average = (total/SIZE) + offset; else average = (total/(now+1)) + offset; if (sensorValue > average) { Serial.print(" AVE: "); Serial.print(average); Serial.print(" Sensor: "); Serial.println(sensorValue); }
if (sensorValue > average+30) { digitalWrite(clapLed1, HIGH); Serial.print(" AVE: "); Serial.print(average); Serial.print(" Sensor: "); Serial.println(sensorValue); //clap *= -1; Serial.println("---Single req"); delay(100);
int inc; for(inc = 0; inc<2000; inc++) { sensorValue = analogRead(inPin0); //insert the sensor value into the array if(initialized){ total = total - buffer[now] + sensorValue; } else{ total = total + sensorValue;
if(now + 1 >= SIZE) initialized = true; }
//Find the average room noise level if(initialized) average = (total/SIZE) + offset; else average = (total/(now+1)) + offset;
if (sensorValue > average) { digitalWrite(clapLed2, HIGH); Serial.print("--- AVE: "); Serial.print(average); Serial.print(" Sensor: "); Serial.println(sensorValue); clap *= -1; Serial.println("--------Double Clap");
if(clap == 1){ digitalWrite(out, HIGH); // set the LED on } else{ digitalWrite(out, LOW); // set the LED off } delay(1000); break; } buffer[now] = sensorValue; now = (now+1)%SIZE; } Serial.println("---Single ack"); delay(500); Serial.println("done"); } buffer[now] = sensorValue; now = (now+1)%SIZE;
if(average < 0 || average > 800) { initialized = false; for(now = 0;now < SIZE; now++) { buffer[now] = 0; } digitalWrite(out, LOW); total = 0; now = 0; average =0; Serial.print(average); Serial.println("RESET");
} } else { digitalWrite(3, LOW); Serial.println("disabled "); } digitalWrite(clapLed1, LOW); digitalWrite(clapLed2, LOW);} Here's the schematic:
(Note: Vcc in the arduino schematic refers to the 5V supplied from left pin and same with GND)
