Normally any embedded system has relations with the real world. The simplest and most common way is using buttons.
This is how applications interact with humans. But real-world like a human is not perfect. Practically mechanical contacts bounce (turns on and off repeatedly for a short period). And you cannot do anything about it. There is no need to look for higher quality buttons or try special button construction or even try to press the button precisely for simple applications. It is better to deal with it wisely. In digital circuits, there may be used special circuits like triggers, RC circuits, etc. But when working with microcontrollers, there is simpler to remove bouncing by adding a couple of code lines. Usually, switches bounce for less than 20ms; bigger ones may take up to 50ms.
Buttons in embedded systems may cause a number of problems like:
- Rather reading value ‘A’ microcontroller may read ‘AAAAAA’;
- When the button is released, it bounces two – this may look like the button was pressed again after release.
To avoid this use simple routine:
- Read a pin;
- If the switch were detected – wait for 20ms and then reread pin;
- If the second reading confirms the first reading, then assume that button was pressed.
Select delay time according to buttons you use. If 20ms doesn’t guarantee stability, then add more ms. You may determine this time by using an oscilloscope.