Putting robotics at your service™

Free shipping on orders over $200

How to Debounce a PS2 Button

Print view Share :
Previous topicNext topic

Page 1 of 1 [ 4 posts ]

User avatar
Guru ( offline )
Posts: 2949
Posted: 2010-06-06 20:14 
I'm using the Lynxmotion Wireless PS2 controller with an Atom Pro. I've been trying to figure out how to debounce the PS2 buttons. It looks like either the controller or the receiver buffer button presses, and I just want one press per button at a time. The buttons also repeat very quickly, which adds to the problem.

Here is what I came up with. It works great no matter how long you hold a button down and does not affect other button presses.
Code:
   if Button_A and (ButtonBounces = 0) then
      if Autonomous then
         Autonomous = 0
         serout S_OUT,i9600,["Remote control..",13]
      else
         Autonomous = 1
         serout S_OUT,i9600,["Autonomous..",13]
      endif
      
      ButtonBounces = ButtonBounces + 1
   else
      if Button_A then
         ButtonBounces = ButtonBounces + 1
      else
         ButtonBounces = 0
      endif
   endif


I also have:
Code:
ButtonBounces var word
ButtonBounces = 0

8-Dale

_________________
I can usually handle complexity. It's the [b][i]simple[/i][/b] things that always confound me..
Check out my [url=http://www.hybotics.me]blog[/url] now!
Open your mind, Read, Learn, Think, Apply..


User avatar
Guru ( offline )
Posts: 4856
Posted: 2010-06-07 09:32 
Hi Dale,

Looks good. So far I have not had any problems with the way the several programs up here deal with only processing a button once. That is they remember the previous button states and only process the button when that state changes.
Code:
 ...
   shiftin PS2DAT,PS2CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
   DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
   high PS2SEL
   pause 10   
 
   IF (DualShock(1).bit3 = 0) and LastButton(0).bit3 THEN   ;Start Button test
      IF(RoverOn) THEN
         ;Turn Rover off
         RoverOn = False
      ELSE
         ;Turn Rover on
         RoverOn = True   
      ENDIF
   ENDIF
...
     LastButton(0) = DualShock(1)
     LastButton(1) = DualShock(2)
RETURN


Kurt


User avatar
Guru ( offline )
Posts: 2949
Posted: 2010-06-07 10:24 
I believe my code can be simplified a bit, and I want to get rid of the counter.

8-Dale

_________________
I can usually handle complexity. It's the [b][i]simple[/i][/b] things that always confound me..
Check out my [url=http://www.hybotics.me]blog[/url] now!
Open your mind, Read, Learn, Think, Apply..


User avatar
Guru ( offline )
Posts: 2949
Posted: 2010-06-07 11:46 
kurte wrote:
Looks good. So far I have not had any problems with the way the several programs up here deal with only processing a button once. That is they remember the previous button states and only process the button when that state changes.
Code:
 ...
   shiftin PS2DAT,PS2CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
   DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
   high PS2SEL
   pause 10   
 
   IF (DualShock(1).bit3 = 0) and LastButton(0).bit3 THEN   ;Start Button test
      IF(RoverOn) THEN
         ;Turn Rover off
         RoverOn = False
      ELSE
         ;Turn Rover on
         RoverOn = True   
      ENDIF
   ENDIF
...
     LastButton(0) = DualShock(1)
     LastButton(1) = DualShock(2)
RETURN

I don't really get how I could apply this to what I am doing. I don't understand what you are doing in your code.

Here is my latest code. No rocket science. I just replaced the counter with a bit variable.
Code:
   ' Remote / Automomous Control Toggle
   if Button_A and A_Enable then
      if Autonomous then
         Autonomous = 0
         serout S_OUT,i9600,["Remote control..",13]
      else
         Autonomous = 1
         serout S_OUT,i9600,["Autonomous..",13]
      endif
      
      A_Enable = FALSE
   else
      if Button_A then
         A_Enable = FALSE
      else
         A_Enable = TRUE
      endif
   endif

It works for me, and I understand what is going on, so will likely stay with it.

8-Dale

_________________
I can usually handle complexity. It's the [b][i]simple[/i][/b] things that always confound me..
Check out my [url=http://www.hybotics.me]blog[/url] now!
Open your mind, Read, Learn, Think, Apply..


 

All times are UTC - 5 hours [ DST ]. It is currently 2014-07-10 22:18
Feedback Form
Feedback Form