Shop OBEX P1 Docs P2 Docs Learn Events
Chapter 03 - Reaction Timer Game - Alternate program with a different Circuit — Parallax Forums

Chapter 03 - Reaction Timer Game - Alternate program with a different Circuit

AndyMenonAndyMenon Posts: 26
edited 2013-04-22 07:09 in BASIC Stamp
Hi Folks,

Total Parallax Noobie here icon7.png If I've posted this in the wrong section, I apologize in advance.

Here's my first attempt at writing a program for the Reaction Timer Game from Chapter 03 in the Parallax Basic Stamp "What's a Microcontroller" book.
For those who're already familiar with this, I haven't yet included the random timer feature because I wanted this program to swith LED colors within the predicatable time of 1 second to test the cheat feature.

Here are some of my changes:
  1. I lost the Bi-Color LED, and therefore used the Dual LED program from Chapter 03 with the pull-down resistor. Pic is attached .
    • For now, the upper push button has no part to play in the game, and can therefore be ignored
  2. My program manipulates the 2 LEDs to change colors instead of working with a single Bi-Color LED.
  3. Also, I have incorporated an alternate solution to detect if the player is letting go of the button before the LED turns yellow and displays a message appropriately.
Please let me know what you think. Here's the pic below, and the code has been attached to this post as well.03_05_ReactionTimer_Game_Program.bs2

Cheers,
Andy

ReactionTimer-Dual-LEDs.jpg

Comments

  • Daveb1972Daveb1972 Posts: 1
    edited 2013-04-06 19:22
    I had similar aspirations, and came up with this code, that also flashed the winning side when played away from the computer.
    ' What's a Microcontroller - ReactionTimerRandomTwoPlayer.Bs2
    ' Test random reaction time with a pushbutton and a Bicolor LED.
    ' Added second player, with second pushbutton. Both players
    ' play at once using the same LED. Quikest to release wins. Includes random switchtime, and anti cheat code.
    ' Pin P3: Player A pushbutton, Active High
    ' Pin P4: Player B pushbutton, Active High
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    PAUSE 1000
    timecounter VAR Byte
    timecounter2 VAR Byte
    timecounterA VAR Word
    timecounterB VAR Word
    value VAR Byte
    value = 23
    DEBUG "Press and hold Pushbuttons", CR,
          "to make the LED turn Red",CR ,CR,
          "When LED turns Green, let",CR,
          "go as fast as you can.", CR, CR
    DO
      DO
      LOOP UNTIL (IN3 = 1) AND (IN4 = 1)
        HIGH 14
        LOW 15
        RANDOM value
        DEBUG "Delay: ", ? 1000 + (value*4), CR
        PAUSE 1000 + (value*4)
        LOW 14
        HIGH 15
      timecounterA = 0
      timecounterB = 0
      DO
        PAUSE 1
        IF (IN3 = 1) THEN
         timecounterA = timecounterA + 1
        ENDIF
        IF (IN4 = 1) THEN
         timecounterB = timecounterB + 1
        ENDIF
    
      LOOP UNTIL (IN3 = 0) AND (IN4 = 0)
        IF (timecounterA <=2) OR (timecounterB <=2) THEN
          DEBUG CLS, HOME, "You Must wait for the LED to change to Green...FAIL!", CR, CR
                 timecounter2 = 0
          DO
            DEBUG "NO CHEATING!", CR
            LOW 15
            HIGH 14
            PAUSE 250
            HIGH 15
            LOW 14
            PAUSE 250
            timecounter2 = timecounter2 + 1
          LOOP UNTIL timecounter2 = 20
            DEBUG CLS, HOME, "To play again, hold the", CR,
                  "button down.", CR, CR
          ELSEIF (timecounterA >1) AND (timecounterB >1) THEN
            LOW 15
            LOW 14
    
            timecounterA = timecounterA */254
            timecounterB = timecounterB */254
            DEBUG "Player A Time: ", DEC timecounterA," ms.", CR, CR,
                  "Player B Time: ", DEC timecounterB," ms.", CR, CR
            IF (timecounterA < timecounterB) THEN
            DEBUG "Player A Wins!!!", CR
            timecounter = 0
            DO
            LOW 15
            HIGH 14
            PAUSE 250
            LOW 14
            PAUSE 125
            timecounter = timecounter + 1
            LOOP UNTIL timecounter = 10
            ELSEIF (timecounterB < timecounterA) THEN
            DEBUG "Player B Wins!!!", CR
            timecounter = 0
            DO
            LOW 14
            HIGH 15
            PAUSE 250
            LOW 15
            PAUSE 125
            timecounter = timecounter + 1
            LOOP UNTIL timecounter = 10
            ELSE
            DEBUG "It's a Tie!", CR
             timecounter = 0
            DO
            LOW 15
            HIGH 14
            PAUSE 125
            HIGH 15
            LOW 14
            PAUSE 125
            timecounter = timecounter + 1
            LOOP UNTIL timecounter = 10
    
            LOW 15
            LOW 14
            ENDIF
          ELSE
          DEBUG "To play again, hold the", CR,
                "buttons down again.", CR, CR
          ENDIF
          LOW 14
          LOW 15
    LOOP
    
  • AndyMenonAndyMenon Posts: 26
    edited 2013-04-07 12:20
    Nice! The reason I wrote my code the way I did is because when reading through the ReactionTimer example in the BasicStamp Text, the Item3 fix where the program detects the player is cheating using that 2 second check kind of confused me.

    Here's what I mean - In the pseudocode section, we have 3 items to fix

    Item 1 Fix: Removing the PAUSE 1 inside the FOR LOOP removes the code overage
    Item 2 Fix: Adding randomness to the LED change time, increases the pause time from 1 to 2 seconds
    Item 3 Fix: In the pseudocode section there are 2 conditions:

    If timeCounter <=2
    Display Cheat Message
    Else (if timeCounter > 1)
    Display ReactionTime

    But when timeCounter > 1, it may also mean that it can fall under the first condition timeCounter <=2, in which case the program never gets to go into the Else condition.
    Am I reading this wrong, or does my reasoning make sense?

    Thanks and Happy Coding!
    Andy
  • JLockeJLocke Posts: 354
    edited 2013-04-22 07:09
    It would sure seem so. But I think the order of execution is important here. The apparent 'failing' case is when timeCounter = 2. In that instance, timeCounter IS less than or equal to 2, AND timeCounter is greater than 1. But if you look at the code, when timeCounter equals 1 or 2, then the first condition (timeCounter <= 2) is TRUE and DisplayCheatMessage is called and the 'if' test ends ('Else' is never run, because the 'If' condition was TRUE). If timeCounter is > 2, then the first condition is FALSE, but the 'else' condition is still TRUE (timeCounter > 1), so DisplayReactionTime is called.

    Does that make sense?
Sign In or Register to comment.