I have made a (reasonably) reliable clapper!
I used a circuit that I made myself to connect the microphone.
I know there are better circuits, but I didn't have an amp chip thing.
Here's the circuit:
5v
|
|----------|
| C O mike
|)--B------|
| E
|
analog 0-----*------ww------|i ground
10k
Here's the code:
int def;
int on = false;
int switch1 = false;
int switch2 = false;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
delay(250);
int num1 = analogRead(0);
delay(250);
int num2 = analogRead(0);
delay(250);
int num3 = analogRead(0);
def = (num1 + num2 + num3) / 3;
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
void loop() {
if(abs(def - analogRead(0)) > 3) {
delay(200);
if(abs(def - analogRead(0)) > 3) {
switch1 = false;
}
else {
switch1 = true;
}
long time = millis();
while((abs(def - analogRead(0)) < 3) && millis() <= time + 1000) {
}
if(abs(def - analogRead(0)) > 3) {
switch2 = true;
}
else {
switch2 = false;
}
if(switch1 && switch2) {
if(on == false) {
digitalWrite(13, HIGH);
on = true;
}
else {
digitalWrite(13, LOW);
on = false;
}
}
}
}
So far, it turns the led on or off with 2 claps.
It could easily be modified to work with 2 or more leds, where to light the second you clapped thrice (?) etc.