Debouncing button command

gtslabs

Member
I am trying to get an output to go high while a button is depressed then low when released. Sounds simple but I have not had much luck getting it to work using the delays recommended in the manual. Before I go to a RC debounce I wanted to get some input on my code and possibly how to implement the debounce command for this situation. I am using the 40X.

I loop thru my main loop looking at the input from a 3x3 array of buttons held high.
then gosub to the code for that button.
So when I depress and hold the Jog_Up_Button it should set the "enable" output pin high then set it low once released or if a limit switch is triggered.
Below is part of my code.
thanks
Steve

<code><pre><font size=2 face='Courier'>

'Input Pins
'3 x 3 cluster of Input Switches

Symbol Jog_Up_Button = Pin7
'more buttons here
'
'

'Limit Switches
Symbol Limit_Retracted = Pin4 'Must be prefixed with Portc
Symbol Limit_Extended = Pin5 'Must be prefixed with Portc


'Define Output Pins
Symbol Enable = 0
Symbol Direction = 1


'Define Constants
Symbol Down = 1
Symbol Up = 0
Symbol Yes = 1
Symbol No = 0
Symbol Tripped = 1
Symbol Open = 0

'Initialze the motor with the Enable Pin off
Low Enable
Limit_Extended = Open 'Test for both limt switch conditions at startup.

Main:

'All buttons are pulled high - Logic Low is depressed.
If Jog_Up_Button = 0 then gosub Jog_Up
'Loop thru all innput buttons
goto Main



Jog_Up:

LOW Direction
High Enable

do
If Limit_Retracted = Tripped then Exit 'Stop if Exteneded Limit Switch is tripped
Pause 25
Loop Until Jog_Up_Button = Down 'Loop WHILE Jog_Down_Up is depressed
Low Enable 'Turn off Enabled
Return

</font></pre></code>
 

Michael 2727

Senior Member
The simplest way I have found to debounce a switch is jus to place a 0.1&#181;F capacitor across the switch terminals, a greencap or monolithic (N/P) will work fine.
Unless your switch is exceptionally noisy this should work in 95% of applications.
 
Top