Hi Jim, and Happy New Year!
Your issue does sound familiar, yes.
I think what you're seeing is called
'switch bounce'.
All mechanical switch devices do it to some extent.
When you press the button, the metal contacts don't close cleanly...they continue to 'bounce' for a short time afterwards - maybe only for a few milliseconds, but enough to send a number of confusing pulses to the Arduino, rather than the desired single, clean pulse.
Switch debouncing is a big topic all on it's own, and I'm not an expert by any means
(as the title to this thread shows!), but suffice to say that there are several ways to deal with it.
- Software debouncing
- Hardware debouncing
Here is a link to a very helpful Hackaday page which neatly sums up the possible options -
LINK
The author (Elliot) discusses that the best method is hardware debouncing using a combination of an RC circuit and a Schmitt trigger.
But for our purposes we could probably get away with just a simple RC circuit.
"What's an RC circuit?"
It's a very simple circuit that combines specific values of a resistor and a capacitor
(R=resistor, C=capacitor)
Depending on the values chosen, you will get a different time constant...this is the bit we're interested in.
The forumula looks like this:
τ = RC
(Where τ is the time constant)
So, lets say that we want a time constant of 1 millisecond
(which should be enough time for the switch contacts to settle down)
Lets put a 10 KΩ resistor and a 0.1 μF (100nF) capacitor into the formula...
That gives us our desired 1 millisecond time constant...perfect!
Here is a link to a handy Digikey RC time constant calculator
(the one I've just used to do this calculation) -
LINK
Now simply add these components to your switch as in the following diagram:
This is the simplest
(but least effective) method of debouncing...but I think it should be fine for our purposes...though, as I said, I'm no expert!!
It relies on the discharging of the capacitor to create a 'pulse' for the Arduino
(yes...it's not a pulse as such - it's a discharge curve), and so the Arduino will only see one 'pulse' and while the capacitor is recharging, any other pulses
(or bounces) will not be seen by Arduino.
The bigger the value of capacitor
(and/or resistor), the longer the time constant.
I have tried messing around with Schmitt triggers, and while they do work well, it's yet another IC component and associated jumpers etc, that my tiny brains have trouble with!!
But briefly, they create a clean, single rising or falling edge, 'filtering' out all the bounces, depending on thresholds etc....it gets a bit complicated, so I'll stop there!!
This might seem like too much hard work, and you might elect to just go with repeated presses of the button til you get a nice collision
(I did with my MK I drop controller )
Hope this has been helpful Jim, best of luck!