Shop OBEX P1 Docs P2 Docs Learn Events
Displaying time in seconds. — Parallax Forums

Displaying time in seconds.

JAMB1234JAMB1234 Posts: 8
edited 2008-10-07 17:43 in BASIC Stamp
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

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-10-07 13:57
    Hi JAMB1234, to derive seconds from milliseconds divide by 1000

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-10-07 14:14
    Unsoundcode's suggestion needs one more thing ... Use DEBUG DEC seconds,".",DEC3 milliseconds
    You can combine everything and avoid more variables by doing: DEBUG DEC timecounter/1000,".",DEC3 timecounter//1000
  • JAMB1234JAMB1234 Posts: 8
    edited 2008-10-07 14:18
    Thanks alot for your quick response Jeff. I understand now how to derive seconds from milliseconds, I'm still not sure where to place this into my code.

    Thanks again
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-10-07 16:53
    Hi,

    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
  • JAMB1234JAMB1234 Posts: 8
    edited 2008-10-07 17:43
    Thanks to you both, You've been a great help.
Sign In or Register to comment.