Creating Your Own Keyboard From Scratch – Key Debouncing

Summary Page

In my last post, I explained how I scanned the keyboard matrix, in the code snippet shown I omitted a few things. Most importantly the debouncing of the keys. That is truly a critical step. If you don’t get it right you will have more letters on your screen then you intended. Every bounce might get counted and every key press might result in 2, 3 or 4 letters being send to the computer. But wait, what is (de-)bouncing actually? Hopefully this explanation is somewhat comprehensible: whenever you close an electric circuit by contacting two parts of a conductive material, they always don’t instantly close, instead they swing back and forth (at an atomic level), closing and releasing the connection a few times before they rest and establish a lasting one. The problem is when the key matrix scanning is fast enough it will catch the swinging (bouncing) and react as if the button is released and then pressed again (which is actually the case). The Arduino is fast enough! Even when using those Cherry keys, that have a comparatively short bounce time of 5ms. There are a few strategies to handle bouncing. For an in-depth discussion you might find this site interesting. I decided to implement the debouncing in software. There, I feel truly home. I found the Arduino debouncing library. But as it is designed to debounce only single buttons directly connected to the GPIOs of the Arduino and it felt quite cumbersome to bend it to be usable with my keyboard matrix, so I decided to implement my own debouncing strategy. Just in case you didn’t read my previous post here is the matrix scanning function: [gist 3917754/] Software debouncing basically means: when a key has changed it’s state ignore for some time (the bounce time) subsequent state changes. So here follows the actual debounce code: [gist 3919048/] The “bounceTimes” array is responsible for storing timestamps of all bouncing keys. When a state change of a key is found by scanning the key matrix the function checks if the key is bouncing. In case it is not bouncing, the change is acknowledged, the current timestamp gets stored in the “bounceTimes” array and the index of the array field gets stored in the “Key” objects “bounceKey” field. In case it is bouncing, nothing happens. The lookup if a key is bouncing gets handled by the function “checkStillBouncing”. The value of the “bounceKey” field of the “Key” object is its parameter. If the value is “NOT_BOUNCING” than false gets returned. If it is a different value, the timestamp from the “bounceTimes” array at index “bounceKey” is retrieved. If the current timestamp minus the retrieved value is bigger than “BOUNCE_TIME” than “false” gets returned, as the key has probably finished bouncing, otherwise “true” is returned. You might argue, that I could have stored the timestamp directly in the “Key” object. That would have made the implementation a lot simpler. But as the Arduinos working memory is quite limited I decided for the presented solution. I safe a lot of memory this way as each key now only stores an one byte integer instead of a four byte long value. As I succinctly added further functionality I never encountered low memory problems. I never calculated the memory consumption, though, so the simpler solution might have been enough.

Summary Page

One thought on “Creating Your Own Keyboard From Scratch – Key Debouncing

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.