Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp Mini Game — Parallax Forums

Basic Stamp Mini Game

xanaduxanadu Posts: 3,347
edited 2013-09-17 16:24 in Robotics
Test your reflexes using the mighty BS2 :)

How it works:

0. Connect a button to P10 (pulled low when closed), Connect LED to P0, Connect Piezo to P1
1. Change Skill Level Variable in code if needed
2. Run the code below on your BS2
3. When the LED is on it is the beginning of the round (also determining random pause time)
4. Press the button to Start (LED goes out)
5. Wait for the LED to flash when it does press button to win!
6. If you miss or press to soon you lose round
6. Debug terminal keeps score

IMG_2119.jpg


Build the circuit as pictured.

Use this code:
'{$STAMP BS2}
'{$PBASIC 2.5}


' -----[ Program Description ]---------------------------------------------


' Test your reflexes BS2 game.
' Button is pulled low on pin 10, Use LED and resistor on pin 0, connect
' piezo buzzer to pin 1.




' -----[ I/O Definitions ]-------------------------------------------------


B01            PIN     10                    ' Button
LED            PIN     0                     ' LED
BUZZER         PIN     1                     ' Piezo


' -----[ Variables ]-------------------------------------------------------


SkillLevel VAR Word                          ' Skill level Varibable
PValue VAR Word                              ' Pause value Varibable for random pause
Counter VAR Word                             ' Counter Varibable for random LED flash
Won VAR Word                                 ' Wins Varibable for score
Lost VAR Word                                ' Losses Varibable for score


' -----[ Init Setup ]------------------------------------------------------


Won = 0                                      ' Initialize Score Variables
Lost = 0


SkillLevel = 150                             ' Set skill level here (lower is harder)
                                             ' ~100 minimum for most push buttons


' -----[ Program Code ]----------------------------------------------------


DEBUG CLS                                    ' Clear the debug terminal
DEBUG "Skill Level "
DEBUG DEC SkillLevel, "!  "                  ' Display skill level variable
DEBUG "Press Button to Start Round "
DEBUG HOME, LF, "Won: "                      ' Show initial score
DEBUG HOME, LF, LF, "Lost: "




game:
DO                                           ' Game start


DO UNTIL B01 = 0                             ' Loop until button is pressed
HIGH LED                                     ' LED on
RANDOM PValue                                ' Randomize PValue Varibable
LOOP


PAUSE 500                                    ' Give time for button release


LOW LED                                      ' Turn off LED


GOSUB randompause                            ' Begin randompause subroutine


GOSUB flash                                  ' Begin flash subroutine


LOOP


randompause:                                 ' Loops button input while waiting for random time
DO UNTIL PValue < 101                        ' to pass before flashing LED
PValue = PValue - 10                         ' Runs down random pause timer fast
IF (B01 = 0) THEN GOTO lose                  ' If button pressed before LED flash, lose round
LOOP
RETURN


flash:                                       ' Flashes LED for skill level time
FOR counter = 0 TO SkillLevel
HIGH LED
IF (B01 = 0) THEN GOSUB win                  ' Detects button press for win
NEXT
GOSUB lose                                   ' No button press, lose


lose:                                        ' Lose subroutine
LOW LED                                      ' Turn off LED
lost = lost + 1                              ' Update score
DEBUG HOME, LF, LF, "Lost: "
DEBUG DEC lost, " "
FREQOUT buzzer, 500, 3000                    ' Play lose melody
FREQOUT buzzer, 1000, 2700
FREQOUT buzzer, 1500, 2500
PAUSE 1000                                   ' Pause a second
GOSUB game                                   ' Go back to game start


win:                                         ' Win subroutine
LOW Led                                      ' Turn off LED
won = won + 1                                ' Update score
DEBUG HOME, LF, "Won: "
DEBUG DEC won, " "
FREQOUT buzzer, 500, 5000                    ' Play win melody
FREQOUT buzzer, 100, 5500
FREQOUT buzzer, 1000, 6000
PAUSE 1000                                   ' Pause a second
GOSUB game                                   ' Go back to game start



Have fun!
1024 x 768 - 84K
Sign In or Register to comment.