Control Flow and Return Endif Outl Essay

Submitted By johnsmithy1
Words: 740
Pages: 3

'******************************************************************************
'******************************************************************************
' Program Name: LED Reaction Game
'
' Program Description: Uses 7 LEDs to create a game
'
' P0 - RED
' P1 - RED
' P2 - RED
' P3 - GREEN
' P4 - RED
' P5 - RED
' P6 - RED
'
' P10 - Button
'
' LEDs turn on then off one at a time P0 - P7
' User has to press the button when the green LED is on
' Then game speeds up
' If button is pressed when Red is on then show score message and End Game
'
' To restart the game press the button again
'
' File Updated from Template.bs2
'
' Author: Mr Birch
'
' Date: Jan 13th 2010
'
'{$STAMP BS2}
'{$PBASIC 2.5}
'******************************************************************************
'******************************************************************************

'******************************************************************************
' Variables
'******************************************************************************
Counter VAR Word ' used in For Next loops Test VAR Bit ' used when checking a button LoopTimer VAR Word ' used in button testing loop BlinkTime VAR Word ' time between each LED turn on and off GameOver VAR Bit ' flag used to see if game is over Score VAR Byte ' used to hold score

'******************************************************************************
' Application Code
'******************************************************************************
MainProgram:

DIRL = %11111111 ' Setup pins 0 to 7 as outputs OUTL = %00000000 ' Turn off all LEDs DIR10 = 0 ' Set pin 10 as input

DO ' Setup Big loop - Game can never End GOSUB GetReady

score = 0 ' Reset score GameOver = 0 ' Set GameOver flag to not over (0) BlinkTime = 100 ' Set initial time for game to 2 seconds

DO WHILE GameOver = 0 ' Run the game while GameOver flag 1 GOSUB LoopLeds ' Run the LoopLEDs Subroutine LOOP

GOSUB ShowScore ' Show Score wait for reset button

LOOP

END ' End the program ' DO NOT DELETE THIS END! ' IT STOPS THE FOLLOWING CODE ' FROM RUNNING AUTOMATICALLY

'******************************************************************************
' Command Subroutines Follow
'******************************************************************************
'
' Get Ready - Code to tell the user to get ready
' Uses a countdown P0 and P7, then P1 and P6, then P2 and P4 Then P5 on
'
GetReady: OUTL = %01000001 PAUSE 500 OUTL = %01000001 PAUSE 500 OUTL = %00100010 PAUSE 500 OUTL = %00010100 PAUSE 500 OUTL = %00001000 PAUSE 500
RETURN

'
' End of Subroutine
'*****************************************************************************
'
' Loop LEDs - Subroutine to turn each LED on /off then check the button
' each section turns on an LED runs the CheckButton subroutine then
' decides if you lost or won
'
LoopLEDs: OUTL = %00000001 ' Turn on LED P0 GOSUB TestButton ' Run CheckButton Sub IF Test = 0 THEN ' If button pressed then lost GOSUB Lose ' so go back to main loop RETURN ENDIF

OUTL = %00000010 ' Turn on LED P1 GOSUB…