Keep repeating the first sequence?

Hi. I have a BBII connected to SSC32 and Playstation 2 wireless remote control.

I exported my project, which containing 3 sequences, to basic atom code using the SSC32; and I tried to assigned 3 different buttons on the controller, each activates different sequence.

The problem is, when I press the button which suppose to activate sequence 2 only, it does this: plays sequence 2 and then keeps playing sequence 1 repeatedly; the loop doesn’t stop when I press other buttons on the controller. Only way to stop it is to reset it or turn off the VL supply.

The same thing happens when I press the button which should only activate sequence 3; it keeps repeating sequence 1 after playing the sequence 3.

I have checked the code many times, but still can’t find out the problems. Could it be the controller problem? Or the basic atom problem?

My code is as follow:

;-----------Bot Board Selection----------
;PS2 Controller / BotBoard I
;DAT con P4
;CMD con P5
;SEL con P6
;CLK con P7

Steps Wordtable $226C, $43E8, $401F, $401F, |
$26B6, $201F, $201F, |
$26B6, $43E8, $401F, $401F, |
$426C, $401F, $401F, |
$459D, $401F, $401F, |
$242E, $4412, $401F, $401F, |
$4580, $401F, $401F, |
$2629, $201F, $201F

SeqPos Wordtable $0000, |
$0007, |
$0011, |
$001B

SSC32 con p8

SeqIndex var Word
Indes var Word
LastServo var Byte
TmpServo var Byte
TmpValue var Word
DejaVu var bit

mot var byte

LastServo = 99
DejaVu = 0

pause 500

;PS2 Controller / BotBoard II
DAT con P12
CMD con P13
SEL con P14
CLK con P15
;-----------------------------

index var byte
temp var byte(19)
mode var byte
Small_Motor var byte
Large_Motor var byte

;PS2Init
high CLK

low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
high SEL
pause 100

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
high SEL
pause 1

main

;-----------PS2 Mode----------
low SEL
; asking “mode” to PS2 controller
shiftout CMD,CLK,FASTLSBPRE,$1\8]
; reading “mode” from PS2 controller
; 73(hex) is dualshock1 (digital buttons)
; 79(hex) is dualshock2 (analog buttons)
shiftin DAT,CLK,FASTLSBPOST,[mode\8]
high SEL
;-----------------------------

pause 1

;-----------PS2 Data----------
low SEL
; asking data to PS2 controller
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]
; reading data from controller
; (don’t use a “for-next” loop, it’s too slow to read PS2 data)
shiftin DAT,CLK,FASTLSBPOST,[temp(0)\8,temp(1)\8,temp(2)\8,temp(3)\8,temp(4)\8,temp(5)\8,temp(6)\8,temp(7)\8,temp(8)\8, |
temp(9)\8,temp(10)\8,temp(11)\8,temp(12)\8,temp(13)\8,temp(14)\8,temp(15)\8,temp(16)\8,temp(17)\8,temp(18)\8]
high SEL
;-----------------------------

pause 1

;-----------PS2 Vibration motors----------
Small_Motor = 1 - Temp(1).bit5 ; Numeric right arrow button (Small Motor on/off)
Large_Motor = Temp(8) ; Analog left arrow button (Large Motor speed)

low SEL
; asking data to PS2 controller (again)
; sending at the same time the vibration motors speed to perform
; (using the previously read analog buttons position ( <- : large motor, -> : small motor )
; the more you press on these buttons, the more the corresponding motor vibrates fast
; some wireless PS2 controllers react only to the large motor command and some have no motor at all.
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8,$0\8,Small_Motor\8,Large_Motor\8]
; we don’t care about reading the data from the controller here.
; so, why not use this to read PS2 data the first time? because it’s not working:
; as the PS2 controller is starting to send data just after receiving the
; “shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8…”<------here
; and as we are sending after that some data for motors “… ,$0\8,temp(7)\8,temp(8)\8]”
; it’s now too late to read all the data from the PS2 controller
; because there’s already some bytes sent by it and lost (no buffer here).
high SEL
;-----------------------------

;-----------Basic Micro IDE terminal----------
; sending carriage return (CR = 13) and the PS2 mode(hex) to the PC
;serout S_OUT,i57600,[13, hex2 mode\2] ; Basic Micro IDE <= 02.2.1.1
;serout S_OUT,I8N1_57600,[13, hex2 mode\2] ; Basic Micro IDE <= 05.3.0.0
serout S_OUT,i57600,[13, hex2 mode\2] ; Basic Micro Pro IDE <= 08.0.1.7

for index = 1 to 18 ; temp(0) contains a dummy variable so we don’t send it to the PC
; sending all the data to the PC
;serout S_OUT,i57600," “, dec3 temp(index)\3] ; Basic Micro IDE <= 02.2.1.1
;serout S_OUT,I8N1_57600,” “, dec3 temp(index)\3] ; Basic Micro IDE <= 05.3.0.0
serout S_OUT,i57600,” ", dec3 temp(index)\3] ; Basic Micro Pro IDE <= 08.0.1.7
next
;-----------------------------

if temp(9)>0 then
SeqIndex = 0
gosub motion
endif

if temp(10)>0 then
SeqIndex = 1
gosub motion
endif

if temp(12)>0 then
SeqIndex = 2
gosub motion
endif

goto main

motion:
For Indes = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
TmpServo = Steps(Indes) >> 11
TmpValue = Steps(Indes) & 2047
if LastServo = TmpServo then
TmpValue = TmpValue << 5
if DejaVu then
Pause TmpValue
LastServo = 99
else
serout SSC32,i38400,“T”, DEC TmpValue, 13]
DejaVu = DejaVu ^ 1
endif
else
TmpValue = TmpValue + 500
serout SSC32,i38400,"#", DEC TmpServo, “P”, DEC TmpValue]
DejaVu = 0
LastServo = TmpServo
endif
next
return

Thanks.

You’re going to need to qualify your detection of the button bits.

if (DualShock(2).bit5 = 0) and LastButton(1).bit5 then …

                .
                .
                .

LastButton(0) = DualShock(1)
LastButton(1) = DualShock(2)

Check out most any of the code generated by Powerpod to see this example. I didn’t see anything like that in a cursory look at your code.

Alan KM6VV

Taking a quick look through the code I don’t see anything that is totally obvious. The main thing though is I noticed you are using the Analog values from the PS2 controller, which is somewhat unusual. Most of us simply use the digital value from the earlier part of the PS2 message. This may not be an issue, but I don’t know how well your PS2 controller will give you zero values.

So a couple of solutions. When in your code you do if temp(9) > 0 I think you are hitting the right button on the left part of the controller. You might try something like:
if temp(9) > 20 then…
Note: 20 is just some threshold number as this value can go from 0-255.

Another option which most of us use is something like:

IF (DualShock(1).bit5 = 0) and LastButton(0).bit5 THEN ;D-Right Button test ... ENDIF
As Alan mentioned, this code is setup to save the two bytes of the previous PS2 message that cooresponds to which buttons are pressed and only processes the button press once.

Kurt

okay, I tried the following code, but the servo doesn’t move at all no matter which button I press.

but if I change back to the code I previously posted, the servo moves, but still having the problem that I mentioned in my first post.

how come?

Thanks

Steps Wordtable $226C, $43E8, $401F, $401F, |
$26B6, $201F, $201F, |
$26B6, $43E8, $401F, $401F, |
$426C, $401F, $401F, |
$459D, $401F, $401F, |
$242E, $4412, $401F, $401F, |
$4580, $401F, $401F, |
$2629, $201F, $201F

SeqPos Wordtable $0000, |
$0007, |
$0011, |
$001B

SSC32 con p8

;PS2 Controller / BotBoard II (PS2 controller => pin 12, 13, 14, 15)
DAT con P12
CMD con P13
SEL con P14
CLK con P15

DeadZone con 28
PadMode con $79

;-------------Variables
DualShock var Byte(7)
DS2Mode var Byte
LastButton var Byte(2)

SeqIndex var Word
Index var Word
LastServo var Byte
TmpServo var Byte
TmpValue var Word
DejaVu var bit

LastServo = 99
DejaVu = 0

;-------------------------------------------------------------------------------------
; Main Loop
Main:

;-------------------------------------------------------------------------------------
; Query the current state of the PS2 buttons.
;Ps2Query
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8]
shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]
shiftin DAT,CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
high SEL
pause 1

if DS2Mode <> PadMode then
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
high SEL
pause 1

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK 
  high SEL 
  pause 1 

  ;low SEL 
  ;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE 
  ;high SEL 
  ;pause 1 

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE 
  high SEL 
  pause 1 

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE 
  high SEL 
  pause 1 

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT 
  high SEL 
  pause 288 

  goto main 

endif

;------------------------------------------------------------------------------------------------------------
; Now process the new PS2 input.
if (DualShock(2).bit3 = 0) and LastButton(1).bit3 then ;R1 Button test
;
elseif (DualShock(2).bit2 = 0) and LastButton(1).bit2 ;L1 Button test
;
elseif (DualShock(2).bit1 = 0) and LastButton(1).bit1 ;R2 Button test
;
elseif (DualShock(2).bit0 = 0) and LastButton(1).bit0 ;L2 Button test
;
elseif (DualShock(2).bit4 = 0) and LastButton(1).bit4 ;Triangle Button test
seqIndex = 0
Gosub PlaySeq

elseif (DualShock(2).bit6 = 0) and LastButton(1).bit6 ;Cross Button test
seqIndex = 1
Gosub PlaySeq
elseif (DualShock(2).bit7 = 0) and LastButton(1).bit7 ;Square Button test
seqIndex = 2
Gosub PlaySeq
elseif (DualShock(1).bit0 = 0) and LastButton(0).bit0 ;Select Button test
;
elseif (DualShock(1).bit3 = 0) and LastButton(0).bit3 ;Start Button test
;
elseif (DualShock(1).bit1 = 0) and LastButton(0).bit1 ;L3 Button test
;
elseif (DualShock(1).bit2 = 0) and LastButton(0).bit2 ;R3 Button test (Horn)
;
elseif (DualShock(1).bit4 = 0) and LastButton(0).bit4 ;Up Button test
;
elseif (DualShock(1).bit6 = 0) and LastButton(0).bit6 ;Down Button test
;
elseif (DualShock(1).bit5 = 0) and LastButton(0).bit5 ;Right Button test
;
elseif (DualShock(1).bit7 = 0) and LastButton(0).bit7 ;Left Button test
;
else
; maybe process the 4 analog values from two joysticks

  for Index = 3 to 6 
     if DualShock(Index) > (128 + DeadZone) then 
        DualShock(Index) = DualShock(Index) - DeadZone/2 
     elseif DualShock(Index) < (128 - DeadZone) 
        DualShock(Index) = DualShock(Index) + DeadZone/2 
     else 
        DualShock(Index) = 128 
     endif 
  next 

  ;YSpeed = DualShock(3) - 128      ; Right Stick Horizontal 
  ;XSpeed = DualShock(4) - 128      ; Right Stick Vertical 

  ;Steering = DualShock(5) - 128      ; Left Stick Horizontal 
  ;Height = LastHeight + (((DualShock(6) - 128) / 5) - LastHeight) / StepFlag; Left Stick Vertical

endif

LastButton(0) = DualShock(1)
LastButton(1) = DualShock(2)

goto main

PlaySeq:
For Index = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
TmpServo = Steps(Index) >> 11
TmpValue = Steps(Index) & 2047
if LastServo = TmpServo then
TmpValue = TmpValue << 5
if DejaVu then
Pause TmpValue
LastServo = 99
else
serout SSC32,i38400,“T”, DEC TmpValue, 13]
DejaVu = DejaVu ^ 1
endif
else
TmpValue = TmpValue + 500
serout SSC32,i38400,"#", DEC TmpServo, “P”, DEC TmpValue]
DejaVu = 0
LastServo = TmpServo
endif
next
return

That is hard to say. What have you done to try to debug it? As a side note, It is a lot easier to read the code when you put it in a code block. It helps to preserve the spacing and tabing, such that you can more easily see the logic flow.

So Your code might look more like:

[code]Steps Wordtable $226C, $43E8, $401F, $401F, |
$26B6, $201F, $201F, |
$26B6, $43E8, $401F, $401F, |
$426C, $401F, $401F, |
$459D, $401F, $401F, |
$242E, $4412, $401F, $401F, |
$4580, $401F, $401F, |
$2629, $201F, $201F

SeqPos Wordtable $0000, |
$0007, |
$0011, |
$001B

SSC32 con p8

;PS2 Controller / BotBoard II (PS2 controller => pin 12, 13, 14, 15)
DAT con P12
CMD con P13
SEL con P14
CLK con P15

DeadZone con 28
PadMode con $79

;-------------Variables
DualShock var Byte(7)
DS2Mode var Byte
LastButton var Byte(2)

SeqIndex var Word
Index var Word
LastServo var Byte
TmpServo var Byte
TmpValue var Word
DejaVu var bit

LastServo = 99
DejaVu = 0

;;;;;;;;;;;;;;;;;;;; Added
high PS2CLK
LastButton(0) = 255
LastButton(1) = 255
;;;;;;;;;;;;;;;;;;;; end added

;-------------------------------------------------------------------------------------
; Main Loop
Main:

;-------------------------------------------------------------------------------------
; Query the current state of the PS2 buttons.
;Ps2Query
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8]
shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]
high SEL
pause 1

low SEL 
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8] 
shiftin DAT,CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, | 
DualShock(4)\8, DualShock(5)\8, DualShock(6)\8] 
high SEL 
pause 1 

if DS2Mode <> PadMode then 
	low SEL 
	shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER 
	high SEL 
	pause 1 
	
	low SEL 
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK 
	high SEL 
	pause 1 
	
	;low SEL 
	;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE 
	;high SEL 
	;pause 1 
	
	low SEL 
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE 
	high SEL 
	pause 1 
	
	low SEL 
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE 
	high SEL 
	pause 1 
	
	low SEL 
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT 
	high SEL 
	pause 288 
	
	goto main 
endif 

;------------------------------------------------------------------------------------------------------------
; Now process the new PS2 input.
if (DualShock(2).bit3 = 0) and LastButton(1).bit3 then ;R1 Button test
;
elseif (DualShock(2).bit2 = 0) and LastButton(1).bit2 ;L1 Button test
;
elseif (DualShock(2).bit1 = 0) and LastButton(1).bit1 ;R2 Button test
;
elseif (DualShock(2).bit0 = 0) and LastButton(1).bit0 ;L2 Button test
;
elseif (DualShock(2).bit4 = 0) and LastButton(1).bit4 ;Triangle Button test
seqIndex = 0
Gosub PlaySeq

elseif (DualShock(2).bit6 = 0) and LastButton(1).bit6 ;Cross Button test 
	seqIndex = 1 
	Gosub PlaySeq 
elseif (DualShock(2).bit7 = 0) and LastButton(1).bit7 ;Square Button test 
	seqIndex = 2 
	Gosub PlaySeq 
elseif (DualShock(1).bit0 = 0) and LastButton(0).bit0 ;Select Button test 
; 
elseif (DualShock(1).bit3 = 0) and LastButton(0).bit3 ;Start Button test 
; 
elseif (DualShock(1).bit1 = 0) and LastButton(0).bit1 ;L3 Button test 
; 
elseif (DualShock(1).bit2 = 0) and LastButton(0).bit2 ;R3 Button test (Horn) 
; 
elseif (DualShock(1).bit4 = 0) and LastButton(0).bit4 ;Up Button test 
; 
elseif (DualShock(1).bit6 = 0) and LastButton(0).bit6 ;Down Button test 
; 
elseif (DualShock(1).bit5 = 0) and LastButton(0).bit5 ;Right Button test 
; 
elseif (DualShock(1).bit7 = 0) and LastButton(0).bit7 ;Left Button test 
; 
else 
	; maybe process the 4 analog values from two joysticks 
	
	for Index = 3 to 6 
		if DualShock(Index) > (128 + DeadZone) then 
			DualShock(Index) = DualShock(Index) - DeadZone/2 
		elseif DualShock(Index) < (128 - DeadZone) 
			DualShock(Index) = DualShock(Index) + DeadZone/2 
		else 
			DualShock(Index) = 128 
		endif 
	next 
	
	;YSpeed = DualShock(3) - 128 ; Right Stick Horizontal 
	;XSpeed = DualShock(4) - 128 ; Right Stick Vertical 
	
	;Steering = DualShock(5) - 128 ; Left Stick Horizontal 
	;Height = LastHeight + (((DualShock(6) - 128) / 5) - LastHeight) / StepFlag; Left Stick Vertical 
endif 

LastButton(0) = DualShock(1) 
LastButton(1) = DualShock(2) 

goto main

PlaySeq:
For Index = SeqPos(SeqIndex) to SeqPos(SeqIndex + 1) - 1
TmpServo = Steps(Index) >> 11
TmpValue = Steps(Index) & 2047
if LastServo = TmpServo then
TmpValue = TmpValue << 5
if DejaVu then
Pause TmpValue
LastServo = 99
else
serout SSC32,i38400,“T”, DEC TmpValue, 13]
DejaVu = DejaVu ^ 1
endif
else
TmpValue = TmpValue + 500
serout SSC32,i38400,"#", DEC TmpServo, “P”, DEC TmpValue]
DejaVu = 0
LastServo = TmpServo
endif
next
return [/code]

Note: I notice that all of my programs that use the PS2 have to init the CLK line. I added that in the above code.

When I debug code like this I typically will add debug output strings, that for example dump what I am getting from the PS2. I usually add it just after I read in the PS2 data and just before the code that looks like: now process the new PS2 data.

In fact one program I am playing with right now has code in it that looks like:

#ifdef DEBUGOUTPUT DSChanged = FALSE for i = 0 to 6 if DualShock(i) <> DSLast(i) then DSLast(i) = DualShock(i) DSChanged = TRUE endif next if DSChanged then serout s_out, i9600, "DS: ", hex DualShock(0), " ", hex Dualshock(1), " ", hex DualSHock(2), " ", hex DualShock(3), " ", hex DualSHock(4), | " ", hex DualShock(5), " ", hex DualShock(6), 13] endif #endif
This code requires a couple of variables to be defined (DSLast as an array like DualShock, and DSChanged which is a bit or a byte).

This way you can find out if the button codes are being set properly and that your code is for example not hung up in the PS2 query code.

Kurt

Thank you kurt.

I copied your code with the HI CLK added… but still no response from servo :frowning:

sorry for the code I pasted not in block form, I think it was because I didn’t turn on the html before posting.

I try to see what I can do for the code. Thank you for your help anyway.

You are welcome. The main trick for posting code is you need to put in inside of a [code] block like

; here is the code

There are buttons up at the top of the edit that add the [code] and the like.

Did you try adding any debugging code to find out where you are getting hung up? For example adding a serout to S_OUT at the point where the PS2 input would be processed? Maybe your code is being hung up trying to get your PS2 controller into the right spot?

Maybe add another serout to S_OUT to the beginning of PlaySeq to see if it is called and if so with which SeqIndex.

Kurt