1
\$\begingroup\$

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.enter image description hereenter image description here

\$\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\$
    – Bradman175
    Commented Mar 21, 2017 at 21:13
  • \$\begingroup\$ Yes but I cannot make it ignore interrupts \$\endgroup\$ Commented 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\$
    – JRE
    Commented Mar 21, 2017 at 21:31
  • 6
    \$\begingroup\$ Seriously? A screenshot of code? Something wrong with copy and pasting text? \$\endgroup\$
    – JRE
    Commented Mar 21, 2017 at 21:32
  • 4
    \$\begingroup\$ That is NOT a schematic diagram. It is a wiring diagram. \$\endgroup\$
    – JRE
    Commented Mar 21, 2017 at 21:33

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ great solution! :) \$\endgroup\$ Commented Mar 21, 2017 at 22:06
  • 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\$
    – neonzeon
    Commented 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_G
    Commented Mar 22, 2017 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.