When you simulate a switch turning on, it turns on. Simulate it turning off, and it turns off. In the real world a switch turning on is actually a chaotic mess of stuttering contact and breaks in contact. It's all over the shop. The one and only thing it isn't, is on or off. It's everything except on or off.
It's called switch bounce, and I never saw it when I did my first demo video with the cardboard fish (it's true, that wasn't a real fish, that was just an actor), because the program was doing some linear thing where it moved onto the next bit that was a pause.
With the new chip, the bit that is pausing is different from the bit that's looking for fish pressing levers, and that's different from the bit reporting how many feeds they have had. All this means that a single lever press can be counted as a stack of them.
The the thing that makes me feel best about wasting three days on trying to solve this, is this bit from the last line of my code....
'TODO: work something out to counter switch bounce if required
I made a note of it ages ago, because I read about it and thought it might be an issue some time down the track.
Some days I'm smarter than I am on other days.
The days all look the same from the outside, but there is something different about them.
120 things in 20 years rarely Electronics - Sanity and switch bounce
A simple debouncing technique is something like:
ReplyDeleteif button pressed {
wait for between 5 and 50 milliseconds
if button pressed again {
it probably wasn't noise so do important stuff
}
}
I must have been sick this day as I didnt say thanks.
ReplyDeleteThanks :)
I just realised that my email account for this blog adventure was a throw away one because I wasn't sure if I wanted to attach my real email address(s) to it. The result is that I was never notified of comments. I just used to look at my stats page that had things like comments listed.