# Clap Lamp debounce

I am having problems with code for my clap LED. It switches on and off when I clap, but it bounces, and I cannot find/figure a way to fix this. I think I do know why it is happening. Sound signal is spiky, and it detects more spikes from single clap. I am using Interrupts with Arduino, because if the code is in loop, clap is too short and misses read. How to solve this? Pictures of schematic and code are attached and digital (yellow) is moved to pin 2 for interrupts.

• Once it hears a sound, make the program ignore the sensors for a period of time to ignore the bouncing. Mar 21 '17 at 21:13
• Yes but I cannot make it ignore interrupts Mar 21 '17 at 21:22
• You can't ignore an interrupt, but you can change what you do in the interrupt.
– JRE
Mar 21 '17 at 21:31
• Seriously? A screenshot of code? Something wrong with copy and pasting text?
– JRE
Mar 21 '17 at 21:32
• That is NOT a schematic diagram. It is a wiring diagram.
– JRE
Mar 21 '17 at 21:33

No need for interrupts here.

First remove the digitalWrite from your void loop(){}.

Now, put this in your void loop(){}:

if (digitalRead(trigger)==1) {
state = !state;
digitalWrite(LED,state);
delay(1000);
}


See what I did? The void loop is now super fast, until a sound is detected. At that time, the LED is toggled, followed by a 1s delay to ensure the sound level decays well below the trigger level.

You can experiment to get the delay timing to your liking.

• great solution! :) Mar 21 '17 at 22:06
• To be fair, you could do something very similar using an interrupt. The key point is that, after an event is detected, you must ensure that new events are ignored for a while to let the sound decay below the trigger level. Something like if (ignore != 1) state=!state; in your interrupt routine. Mar 21 '17 at 22:11
• Now you just have to just figure out how to make the light stay on while you are watching that action movie / cop show. Mar 22 '17 at 2:28