If you have an external push button, you add a resistor in series and a capacitor to refV (0V) to it, called "low pass filter", and you are done with the debouncing.
Thank you very much for the great tutorial. Small note: I believe there is no need for the "debouncing code" since the push button in the nucleo f401re board is attached to a pullup resistor (4K7) and 100nF capacitor. Keep up the good work (Y)
You are partly correct. This would filter out any signal with frequency higher than around 2.2kHz. You can check Jack Ganssle's website, there is a article he did on this theme. He came to a conclusion through series of experiments that there are switches that bounce with period greater than 153ms which is of course rare but possible. For microswitch this time can be around 1.2ms ~= 833Hz, this would not be filtered out. So you are correct that this kind of debounce doesn't solve the bouncing problem because the 5us delay is doing the same thing that is already solved with integrated LP filter.
@@Nikola993311 sorry to intrude but you are a recent poster here and seem to know about this stuff. I asked a question of the OP but doubt that he will see it. Can I ask you the same thing please. The question was "@3:33 I have exactly the same problem. My LED is on and then goes off when I press the LED, same as yours. I changed my code to yours and it does exactly the same thing. You say that quote "you can change this by swapping these codes together" I really do not understand what you are saying sorry The second and third lines of the while statement are the same except one has GPIO_PIN_SET and the other GPIO_PIN_RESET. I tried swapping them but it made no difference, Please does someone know how to fix this as I have tried everything. Thanks"
@@MrPnew1 Hi I'll try to help. What he's saying is that he thought that the button is active HIGH i.e. that when he press the button the pin would read '1'(or SET state) and it should be connected like that if we want to preserve energy, but in this case button is active LOW i.e. when he press the button the pin would read '0'(or RESET state). So you should change the GPIO_PIN_SET to GPIO_PIN_RESET inside if statement :). I hope this helped. This way when we press button the LED will be ON and when the button is not pressed the LED will be off and not the other way round.
For toggling the pin you can also use this kind of Approach //This toogle initialization means wether the led wil be on or off for the first press bool toggle = true; if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) { toggle = !toggle; //This delay here because the frequency of the board is so high so you need time to release your finger. //Else you wouldn't be able to see any change of the led HAL_Delay(100); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, toggle); }
Sir, i have a question, that whether in industry, where there are a lot of sensors and actuators, doe they use HAL library, or bare metal, or both are used according to application demand?
Hi, HAL is just part of the BSP (Board Support Package), STM at this case. If it is reliable enough for the intended application, which in practice it is, yes it is widely used.
Hi, Thx for this tutorial. I have done this but i have error with If...else . That says : else is error Syntax. I'm new with programming and i cannot find the reason.
helo, very good tutorials, but I have question, why dont work if I want the first example to try out....but in default state I want to have the LED in OFF position and only ON(LED) when I push the button
thank you for the response. So,... there are different state in default when is the whole board in high or low voltage ? Or something else is to determine why is in default state the LED is ON when start the program at 3:28 in the video? something in setting is PULL HIGH or something this ? I modified the program, the if is now not in == but is != ...and now work as I want. if( HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) != GPIO_PIN_SET)