' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ' ³ ³ ' ³ S I M O N G A M E ³ ' ³ ³ ' ³ For the BASIC Stamp ³ ' ³ ³ ' ³ To build the hardware: ³ ' ³ ³ ' ³ Duplicate the schematic below for each of the first four I/O ³ ' ³ lines (0-3). Each LED should be a different color. ³ ' ³ ³ ' ³ 5V ³ ' ³ ³ ³ ' ³ LED ± ³ ' ³ Û ³ ' ³ ³ ³ ' ³ 220ê ± Momentary ³ ' ³ ± Switch ³ ' ³ ³ ÄÁÄ ³ ' ³ I/O ÄÄÄÄÁÄÄÄÄ ÄÄÄÄ GND ³ ' ³ ³ ' ³ Connect a speaker to I/O line 4 as shown below. ³ ' ³ ³ ' ³ 10æF ÛÛ ³ ' ³ I/O ÄÄÄÄ´ÃÄÄÄÄÄÄÛÛÛ 40ê- ³ ' ³ + GND ÄÄÛÛÛ 50ê ³ ' ³ ÛÛ ³ ' ³ ³ ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ symbol sound_flag = bit0 symbol recite_flag = bit1 symbol x = b1 symbol y = b2 symbol z = b3 symbol note_count = b4 symbol note = b5 symbol rnd = w3 symbol rnd_seed = w4 ' ' ' Attract mode ' attract: sound_flag = 0 'turn off sound for attract mode for z = 1 to 100 'give user a chance to play (again) rnd = rnd + 1 'increment random seed to scramble gosub play_note 'flash a light gosub read_keys 'check for a keypress if y<>4 then play_game 'if keypress, play game next z 'if for-next is done, power-down ' ' ' Power-down mode - wait for keypress ' port = %1111111111111111 'make all pins high outputs to save 'power (no lines should float) power_down_mode: nap 7 'nap for ~2.3 seconds to save power gosub read_keys 'check for any keys pressed if y=4 then power_down_mode 'if no key pressed, nap again ' ' ' Play game ' play_game: sound_flag = 1 'turn on sound for play mode rnd_seed = rnd 'lock current rnd into rnd_seed note_count = 0 'reset note_count sound 4,(70,10,80,10,90,10,100,10) 'make sound to start game pause 1500 'pause before starting add_note: 'main game-playing loop note_count = note_count + 1 'increment note_count (starts at 1) recite_flag = 0 'clear flag to play song w/o recite gosub play_pattern 'play song recite_flag = 1 'set flag to recite song gosub play_pattern 'have user recite song pause 1500 'pause a moment before next round goto add_note 'loop to add note for next round ' ' ' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ' ³ Subroutines ³ ' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ' ' ' Play pattern ' play_pattern: rnd = rnd_seed 'reset rnd to rnd_seed for note = 1 to note_count 'get ready to play/recite song if recite_flag = 0 then pp_recite 'if flag clear, just play gosub get_key 'flag set, wait for user to recite pp_recite: gosub play_note 'play note next note 'do next note if any return ' ' ' Play note ' play_note: x = rnd & %00000011 'get 0 to 3 in x by masking rnd low x 'turn on light x lookup x,(70,80,90,100),y 'lookup note x if sound_flag = 1 then pn_sound 'if sound_flag set then play note y = 0 'clear, do silence instead of tone pn_sound: sound 4,(y,20) 'play note dirs = dirs & %11110000 'make pins inputs (lights off) pause 100 'wait 1/10 second for x = 1 to 7 'get next random in rnd random rnd next x return ' ' ' Wait for keypress ' get_key: for z = 1 to 100 '100 attempts gosub read_keys 'read keys if y < 4 then gk_got 'if key pressed, break out pause 30 'pause 30ms (100*30ms = 3 seconds) next z 'try again if more end_of_game: 'no key - timed out - end of game sound 4,(1,200) 'make bad sound pause 2000 'wait a couple of seconds to allow 'shame to sink in goto attract 'abort subroutine by going straight 'into attract mode - stack can eat 'cake - this doesn't cause an 'eventual stack overflow since it 'is just a 4-nibble circular buffer 'located in W6 gk_got: 'got key, check for expected match y = y ^ rnd & %00000011 'check for match if y <> 0 then end_of_game 'if result not 0, end of game return ' ' ' Read keys into y ' y = 0 to 3 if key pressed, y=4 if no key pressed ' read_keys: x = pins ^ %00001111 & %00001111 'isolate trued key bits y = 4 'default to 4 lookdown x,(%0001,%0010,%0100,%1000),y 'translate pattern to 0-3 return