PDA

View Full Version : Reaction Timer Upgrade!



Steve5591
03-20-2010, 04:36 AM
Here is an upgraded version of the reaction timer, which is a project in “What’s A Microcontroller” book. This upgrade is completely my own idea, but the basic timer programming was used as a starting pointing.· I have added a total of 4 extra features.

1.····· Automatically converting units

2.····· Two-Player game

3.····· Scoring after each player has had a turn

4.····· Finally scoring of two players after ten rounds.

·

This program was written using the Basic Stamp 2, but you might be able to run the program on some of the other stamps with a little bit of work. The hardware connection is easy. Just use the connection for “Reading A Pushbutton…” out of the “What’s A Microcontroller?” book on page 76, and build two circuits. If you connect the bottom pushbutton to P3, and the top to P4, player one, in the Two-Player game, will be the bottom button and player two will be the top one. After you build the pushbutton circuits, connect one bi-color LED to the reaction timer by connecting one lead to P15, and the other to a 470 ohm resistor, and then connect the remaining lead of the resistor to P14. When you use the reaction timer the LED should start red when you press the button and then change to green soon after. If it starts green and changes to red, turn the LED around.

That’s it! Just run the code, follow the instructions, and have fun. It’s not too hard to! Oh, by the way you might need to know the two rules. Don’t release before the LED turns green, and do not hold the button down for a long time. In a Two-Player game, if you release before the green light, your opponent will automatically win! If you hold the pushbutton down for too long, I think over 60 seconds, your time will be off!·

If the in-program instructions seem too short, please bear in mind that text eats up the eeprom, and on my Basic Stamp 2 the eeprom is 99.99% full!

If you have trouble with my reaction timer, please verify that you connected everything correctly and try again.

Okay, copy the code below and run it!









' {$STAMP BS2}

' {$PBASIC 2.5}

timecounter VAR Word

timecounter2 VAR Word

counter VAR Nib

choice VAR Nib

value VAR Byte

check VAR Nib

check2 VAR Nib

loops VAR Nib

check = 0

check2 = 0

loops = 0

value = 50

·

DEBUG CLS, "Push a button to start program then:",CR,

·"1player game = press/hold one button",CR,

·"2player game = press/hold both",CR

·

DO

LOOP UNTIL (IN3 = 1) OR (IN4 = 1)

DEBUG CR, "Initiating program startup... "

·

PAUSE 5000

DEBUG "Ready! What do want to play?"

·

·

DO

LOOP UNTIL (IN3=1) OR (IN4=1)

·

DO

·

counter = counter + 1

·

IF (IN4=1) AND (IN3=1) THEN

DEBUG CLS,"2 player game confirmed! Continue holding",CR

choice = 1

·

ELSEIF (IN3=1)OR (IN4=1) THEN

DEBUG CLS,"1 player game confirmed! Continue holding",CR

choice = 2

·

ENDIF

PAUSE 100

·

LOOP UNTIL (counter = 10)

DEBUG "Release now!",CR

·

DO

LOOP UNTIL (IN4 = 0) AND (IN3 = 0)

·

·

DEBUG "Push the bottom button and release as fast as you can when the led turns",CR,

"green. Repeat as many times as desired.",CR

·

IF (choice = 2) THEN

·

DEBUG "To display your speed on the led, press/release top button."

·

ELSEIF (choice = 1) THEN

·

ENDIF

·

IF (choice = 1) THEN

DEBUG " When the player1 is done, player2 uses the top button."

·

·ENDIF

·

·DEBUG CR,CR

· DO

·

·DO

·LOOP UNTIL IN3 = 1

·

·LOW 14

·HIGH 15

·

·RANDOM value

·PAUSE 1000 + value

·

·HIGH 14

·LOW 15

·

·timecounter = 0

·

DO

·

·timecounter = timecounter +1

·

· LOOP UNTIL IN3 = 0

· IF (loops =>10) THEN

· DEBUG CLS

· loops = 0

· ENDIF

·

· LOW 14

·

· IF (timecounter = 1) THEN

·

· DEBUG "Wait until led turns to let go: "

· ELSE

· ENDIF

·

IF (choice = 2) THEN

·

· DEBUG "Your time was "

·

· ··ELSEIF (choice = 1) THEN

·

DEBUG "Player1's time was "

·

ENDIF

·

· IF (timecounter => 2000) THEN

· DEBUG DEC timecounter/1000, " seconds"

·

· ELSEIF (timecounter <1000) THEN

· DEBUG DEC timecounter," ms"

·

· ELSEIF (timecounter >= 1000) THEN

· DEBUG DEC timecounter/1000," second"

· ENDIF

·

· DEBUG CR

·

·

·IF (choice = 1) THEN

·

‘------------------------------------------------------------------------------------------

·

·

·DO

·LOOP UNTIL IN4 = 1

·

timecounter2 = 0

·

·LOW 14

·HIGH 15

·RANDOM value

·PAUSE 1000 + value

·

·HIGH 14

·LOW 15

·

·timecounter2 = 0

·

·DO

·timecounter2 = timecounter2 +1

·

· LOOP UNTIL IN4 = 0

·

· LOW 14

· IF (timecounter2 = 1) THEN

·

· DEBUG "Wait until led turns to let go: "

·

ELSE

· ENDIF

·

·

· DEBUG "Player2's time was "

·

· IF (timecounter2 => 2000) THEN

· DEBUG DEC timecounter2/1000, " seconds"

·

· ELSEIF (timecounter2 <1000) THEN

· DEBUG DEC timecounter2," ms"

·

· ELSEIF (timecounter2 >= 1000) THEN

· DEBUG DEC timecounter2/1000," second"

· ENDIF

·

·· DEBUG CR

·

·ENDIF

·

·IF (choice = 2)THEN

·

·DO

·LOOP UNTIL (IN4 = 1) OR (IN3 = 1)

·

·IF (IN4 = 1) THEN

·

·HIGH 15

·LOW 14

·

·PAUSE 1000

·

· HIGH 14

· LOW 15

· PAUSE timecounter

· LOW 14

· LOW 15

·

· ELSEIF (IN3 = 1)· THEN

·

· ENDIF

·ELSEIF (choice = 2) THEN

·

·ENDIF

·IF (choice = 1) THEN

·

··· IF (timecounter = 1) THEN

··· DEBUG "Player2 won"

··· check2 = check2 +1

·

··· ELSEIF (timecounter2 = 1) THEN

··· DEBUG "Player1 won"

··· check = check +1

·

··· ELSEIF (timecounter < timecounter2) THEN

··· DEBUG "Player1 won"

··· check = check +1

·

··· ELSEIF (timecounter > timecounter2) THEN

··· DEBUG "Player2 won"

··· check2 = check2 +1

·

··· ELSEIF (timecounter = timecounter2) THEN

··· DEBUG "Tie"

···· check = check + 1

···· check2 = check2 +1

··· ENDIF

·

·

·

·

·

·

·· DEBUG CR,CR

·

·loops = loops + 1

·

·IF (loops = 10) THEN

·

·IF (check > check2) THEN

·DEBUG "Player1 won ",DEC check

·

·ELSEIF (check < check2) THEN

·DEBUG "Player2 won ",DEC check2

·

·ELSE

·

·ENDIF

·

·ELSE

·

ENDIF

ENDIF

·

· IF ((check > check2) OR (check <check2)) AND (loops = 10) THEN

·

· DEBUG "/10 games!",CR,CR

·

·

·