- Mon Oct 23, 2017 9:20 am
#71128
Bouncing, can be solved by adding a small value capacitor (to GND) near the switch, in parallel. I think in general a 10nF or 1nF will be OK.
Whenever you use a loop to check a status of a pin, introduce a variable, which acts like a flag. Use this flag to see if a part of sourcecode should be processed. This flag is also used to prevent a process to be run multiple times (i.e. because bouncing occurs).
Because programming can done in several language, I give a general example.
Define a variable "BtnPushed" and give it default the value "0". What I write below as "sourcecode" is not official. The code is merely Basic for AVR microcontrollers, but you can understand how it operates.
Code: Select allProces CheckBtn()
IF BtnPushed = 0 Then 'Only check if needed : avoid bouncing
IF Input = 1 Then '=Vcc
BtnPushed = 1 'modify variable
Else
'Do nothing
End IF
End IF
End Process CheckBtn()
Proces DoSomething()
For i = 1 to 3
Print "Hello World"
Next
Wait 10 ms 'Wait 10 ms
BtnPushed = 0 'Set it back, so button may be checked again.
End Process DoSomething()
Proces Main()
Call Proces CheckBtn()
IF BtnPushed = 1 Then
Call Proces DoSomething()
Else
'Do Something else
End IF
End Proces Main()
The Main is the loop you always run. One proces is checking if a button is pushed, unless it was recently pushed. The other proces is started to do something when a button was pushed.
There's also something about statusses for a digital INPUT-pin. It's important to have 2 distinct statusses available : Vcc and GND
This means for example, the input is connected to :
1) GND and when pushing the button, it goes to Vcc.
2) Vcc and when pushing a button, it goes to GND.
In many designs, I've seen a button is kept floating (no Gnd nor Vcc), and when pushed it connects to GND or Vcc. This is not a safe design ! Always try to prevent such situation. When an INPUT is kept floating, the wiring between the INPUT and a button acts merely like an "antenna". In other words, a device has to decide itself what the status is. Normally it won't matter much, but if you like it to operate reliable, avoid this situation as a prevention to odd results.
Advise : Make sure there are 2 well defined statusses available for a digital INPUT and don't introduce a third one (floating).
Last edited by oxurane on Mon Oct 23, 2017 10:48 am, edited 1 time in total.
Flex Red Devil. The best programmer available for multi-programming and tinkering with ESP01, ESP03, ESP04, ESP05, ESP07, ESP08, ESP11 and ESP11.