How to Debounce a PS2 Button

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.

	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:

ButtonBounces var word ButtonBounces = 0
8-Dale

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
[/code]

Kurt

I believe my code can be simplified a bit, and I want to get rid of the counter.

8-Dale

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

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

8-Dale