I think that you often run into the problem with MCU that when you press a button it read it not only one time but often even 50 times or more. You probably thought about how to do the so-called "debounce". Here how I am making it, but let me explain why this is happening to you.
First of all the button has two metalic plates which touch each other when the button is pressed. When these metalic plates get at a really close distance it elaborates pikes, so the result will be a lot of touch/untouched signals. Like pressing the button like an idiot a lot of times but this happens in a fraction of second, you know MCU's speed, right?
So here is what you can do, there are several method but I will tell you just two of them.
- first is the schmitt trigger.
- second is using a simple cap.
Schmitt trigger method
This is the best way to make the debouncing. The schmitt trigger calculates all the semnals and using a threshold it will give a simple 1 or 0 to the MCU. This is the best way to make the debounce because using my second method the signal will be even sometimes under 0V like -1V and MCU's doesn't really like it. I have never had problems with AtTiny2313 with the cap.
This is the cheapest method ever had and the seasiest one. All you have to do is attaching in parallel a 0.1uF capacitor. It must not be electrolytic. The debouncing is working perfetly with the cap as can be seen in the video.
I have to say that these are the best methods and the cheapest one. You know hardware debouncing is the best and not the software debounce because it eats a little memory and in a big program it is really probable that you will bang your hand in the monitor because forgotting to re-enable an interrupt or something.