Displaying time in seconds.
JAMB1234
Posts: 8
I'm using the bs2 to run a simple reaction timer similer to the one in the stamps in class book. I was wondering if anyone could help me figure out how to display the time in seconds as opposed to milliseconds. I'm attaching my code.
'{$STAMP BS2}
'{$PBASIC 2.5}
timecounterA VAR······· Word·············· ' Time Score of player A
timecounterB VAR······· Word·············· ' Yime Score of Player B
DEBUG "Press and hold pushbuttons" , CR
DO
·DO
·' Nothing
·LOOP UNTIL (IN10 = 1) AND (IN11 = 1)
LOW 0
PAUSE 4000
LOW 12
PAUSE 4000
LOW 1
PAUSE 5000
LOW 13
PAUSE 1000
HIGH 0
HIGH 1
HIGH 12
HIGH 13
LOW 2
LOW 3
LOW 4
PAUSE 400
HIGH 2
HIGH 3
HIGH 5
HIGH 6
HIGH 4
PAUSE 1
LOW 5
LOW 6
timecounterA = 0
timecounterB = 0
DO
PAUSE 1
IF (IN10 = 1) THEN
timecounterA = timecounterA + 1
ENDIF
IF (IN11 =1) THEN
timecounterB = timecounterB + 1
ENDIF
LOOP UNTIL (IN10 = 0) AND (IN11 = 0)
HIGH 5
HIGH 6
IF (timecounterA =0) THEN
DEBUG " Player A FOUL" , CR
DEBUG " Player B Wins" , CR, CR
LOW 8
PAUSE 4000
HIGH 8
ELSEIF (timecounterB = 0) THEN
DEBUG " Player B FOUL" , CR
DEBUG·· "Player A Wins" , CR
LOW 7
PAUSE 4000
HIGH 7
ELSE
DEBUG "Player A time: ", DEC timecounterA," ms." ,CR
DEBUG "Player B time: ", DEC timecounterB," ms.", CR, CR
IF (timecounterA < timecounterB) THEN
··· DEBUG "Player A is the winner!", CR
LOW 0
PAUSE 2000
HIGH 0
ELSEIF (timecounterB < timecounterA) THEN
DEBUG "Player B is the winner!", CR
LOW 12
PAUSE 2000
HIGH 12
ELSE
DEBUG "It's a Tie!", CR
ENDIF
DEBUG CR
DEBUG "To play again, Hold the", CR
DEBUG "BUTTONS DOWN AGAIN.",CR, CR
ENDIF
LOOP
'{$STAMP BS2}
'{$PBASIC 2.5}
timecounterA VAR······· Word·············· ' Time Score of player A
timecounterB VAR······· Word·············· ' Yime Score of Player B
DEBUG "Press and hold pushbuttons" , CR
DO
·DO
·' Nothing
·LOOP UNTIL (IN10 = 1) AND (IN11 = 1)
LOW 0
PAUSE 4000
LOW 12
PAUSE 4000
LOW 1
PAUSE 5000
LOW 13
PAUSE 1000
HIGH 0
HIGH 1
HIGH 12
HIGH 13
LOW 2
LOW 3
LOW 4
PAUSE 400
HIGH 2
HIGH 3
HIGH 5
HIGH 6
HIGH 4
PAUSE 1
LOW 5
LOW 6
timecounterA = 0
timecounterB = 0
DO
PAUSE 1
IF (IN10 = 1) THEN
timecounterA = timecounterA + 1
ENDIF
IF (IN11 =1) THEN
timecounterB = timecounterB + 1
ENDIF
LOOP UNTIL (IN10 = 0) AND (IN11 = 0)
HIGH 5
HIGH 6
IF (timecounterA =0) THEN
DEBUG " Player A FOUL" , CR
DEBUG " Player B Wins" , CR, CR
LOW 8
PAUSE 4000
HIGH 8
ELSEIF (timecounterB = 0) THEN
DEBUG " Player B FOUL" , CR
DEBUG·· "Player A Wins" , CR
LOW 7
PAUSE 4000
HIGH 7
ELSE
DEBUG "Player A time: ", DEC timecounterA," ms." ,CR
DEBUG "Player B time: ", DEC timecounterB," ms.", CR, CR
IF (timecounterA < timecounterB) THEN
··· DEBUG "Player A is the winner!", CR
LOW 0
PAUSE 2000
HIGH 0
ELSEIF (timecounterB < timecounterA) THEN
DEBUG "Player B is the winner!", CR
LOW 12
PAUSE 2000
HIGH 12
ELSE
DEBUG "It's a Tie!", CR
ENDIF
DEBUG CR
DEBUG "To play again, Hold the", CR
DEBUG "BUTTONS DOWN AGAIN.",CR, CR
ENDIF
LOOP
Comments
seconds=timecounter/1000
take a look at the mod operator in the PBasic help file to derive the remaining milliseconds, the mod operator places the remainder of a division into a variable
milliseconds=timecounter//1000
then format the debug instruction to display the result of both operations
DEBUG DEC seconds,".",DEC milliseconds
JEFF T.
You can combine everything and avoid more variables by doing: DEBUG DEC timecounter/1000,".",DEC3 timecounter//1000
Thanks again
replace
DEBUG "Player A time: ", DEC timecounterA," ms." ,CR
with
seconds=timecounterA/1000
milliseconds=timecounterA//1000
DEBUG "Player A time: ", DEC timecounterA/1000,".",DEC3 timecounterA//1000
same for counter B
Jeff T.
EDIT, Mike's code already took care of the division thus the strikethrough
Post Edited (Unsoundcode) : 10/7/2008 5:04:05 PM GMT