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.
\$\begingroup\$
\$\endgroup\$
6
-
1\$\begingroup\$ Once it hears a sound, make the program ignore the sensors for a period of time to ignore the bouncing. \$\endgroup\$– Bradman175Commented Mar 21, 2017 at 21:13
-
\$\begingroup\$ Yes but I cannot make it ignore interrupts \$\endgroup\$– Willy WonkaCommented Mar 21, 2017 at 21:22
-
1\$\begingroup\$ You can't ignore an interrupt, but you can change what you do in the interrupt. \$\endgroup\$– JRECommented Mar 21, 2017 at 21:31
-
6\$\begingroup\$ Seriously? A screenshot of code? Something wrong with copy and pasting text? \$\endgroup\$– JRECommented Mar 21, 2017 at 21:32
-
4\$\begingroup\$ That is NOT a schematic diagram. It is a wiring diagram. \$\endgroup\$– JRECommented Mar 21, 2017 at 21:33
|
Show 1 more comment
1 Answer
\$\begingroup\$
\$\endgroup\$
3
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.
-
-
2\$\begingroup\$ 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. \$\endgroup\$– neonzeonCommented Mar 21, 2017 at 22:11
-
\$\begingroup\$ Now you just have to just figure out how to make the light stay on while you are watching that action movie / cop show. \$\endgroup\$– Trevor_GCommented Mar 22, 2017 at 2:28