Hey all.
I'm trying to build a clap switch that will detect both single and double claps.
The single claps would trigger one type of load and double claps another type of load.
The logic to do this is the problem.
I have this code that responds to and activates the load for single clap but the double clap triggers both loads.
Can someone help me out please.
I guess they are still working on the code tags because it doesn't work for now.
I'll just put it below.
int reading = 0;
int mic = A2;
int led = 12;
int led2 = 13;
boolean ledOn = 0;
boolean ledOn2 = 0;
boolean flag;
int iChecker =0;
void setup(){
pinMode(mic, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop(){
reading = analogRead(mic);
if (reading > 100){
iChecker = 0;
//flag = false;
delay(200);
for (int i = 0; i < 1000; i++){
reading = analogRead(mic);
delay(1);
if (reading > 100){
ledOn = !ledOn;
//flag = true;
iChecker = 3;
break;
}
}
if (iChecker != 3){
ledOn2 = !ledOn2;
//break;
}
}
if (ledOn == 1){
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
}
if (ledOn2 == 1){
digitalWrite(led2, HIGH);
}
else{
digitalWrite(led2, LOW);
}
}