The robot box needs to know when it has reached the limit of it’s motion. If motion continues past this point, the machine will literally rip itself apart. To stop this happening, I am using a limit switch that will be pressed if the robotic finger move too far. However, it seemed that a good portion of the time the robot would ignore this button and continue to rip itself apart. The reason for this lies in the way I was using interrupts to detect the pressing of the button.
There are two ways for a microcontroller to detect inputs (e.g. buttons) – polling and interrupts. Polling requires the processor to continuously check if the button is pressed. This takes clock cycles away from the main process which may be an issue in some situations. It will also generally not be as responsive, as we have to wait until the microcontroller checks the button before anything can happen.
On the other hand, interrupts rely on special hardware to detect the button being pressed. Regardless of what the microcontroller is doing, an interrupt will force it to handle this event right away.
On an Atmel AVR, like the ATtiny that I am using in the Robox, any one of eight buttons all trigger the same interrupt when pressed, and therefore the only way to know which button has been pressed is to check them all and see once the interrupt has occurred. This is where my issue lay.
It turns out that my buttons are very noisey – that is the contacts bounce a lot when you press the button, resulting a noisey voltage characteristic.
The figure above shows that the button is opening and closing many times, when I only mean it to close once. Therefore, although the interrupt may be triggered at the first falling edge, by the time the microcontroller checks to see if this button is pressed, the switch may have opened once again and the microcontroller will miss it.
This is a typical switch debouncing issue that can be fixed by adding a capacitor to the switch to smooth the voltage.
After adding a capacitor, the voltage characteristic was much smoother during switching and the issue of the microcontroller missing a button press stopped. This confirmed my theory. However, I didn’t want to add capacitors to all the buttons – this seemed like a bodge and I would rather fix the issue in software.
Therefore, instead of relying on interrupts to detect button presses, the processor polls the buttons in it’s main loop. At first glance this feels wasteful, but seeing as the microcontroller has nothing to do until buttons are pressed, we might as well use the main loop for polling and this way I don’t need the capacitors.