STM32 Ultra Basic Kit Special Introductory Price!
ST-Link v2 STM32 and STM8 Programmer
STM32 M0 MCU and Interface to Breadboard
ARM Microcontroller Beginners Kit (STM32F0)
ARM Microcontroller Intermediate Kit (STM32F0)
ARM Microcontroller Advanced Kit (STM32F0)
USB 2.0 Cable Type A Male to Type B Male - 10 FT
Multimeter Security Banana Plug To Test Hook Clip Probe Lead Cable 500V
In the first part, I explained the concept of software debouncing and how mechanical push button create a bounce when the button is pressed. We also wrote some pseudo-code to explain how the flow would happen in the program.
We will start with the variables. The pseudo-code for the variables looked like this:
The use of the volatile keyword is so that the variables do not get optimized out. We want these variables to be dynamic as they are going to constantly change. the use of the variable type char is that it's the smallest common type at only 8-bits. We are only going to store a 0 and 1 in this variable type. This variable type can be either signed or unsigned. If it is signed, it can store values of -128 to +128, otherwise, it will store the unsigned values of 0 to 255. All of the variables will be initialized to 0.
The buttonPressedConfidenceLevel will be an int (integer) since it is a variable that will count the samples of ones (high digital signal level). The samples count can get very high if you let it. Also, another variable is introduced to store the confidence threshold. It is important to mention that the number that you use for the confidence threshold should be changed is the processor speed is faster or slower. The number here should really be based on a variable that contains the main processor speed and the confidence threshold number can be some divisor or coefficient of that.
In the while loop, we can start by changing the buttonPressed variable conditions. If the buttonPressed is equal to zero, then we can add a few updates to variables: the buttonPressedConfidenceLevel can be incremented and the opposite, the buttonReleasedConfidenceLevel can be zero since there is a one at the input pin. However, this needs to be put in the else code block if the confidence level has not reached the threshold. The reason for this is that we don't want the confidence level to increment unnecessarily.
Just because we are updating these confidence level variables doesn't mean that the button is pressed. We can only state the button is pressed if the confidence level becomes greater than the threshold we set.
The same concept is being applied to the area within the buttonPressed == 1 condition, just opposite as we are considering the released rather than the pressed confidence level.