simon memory game?
mrsteele
Posts: 4
im in school and we are working with the·bs2 microcontroller we have been trying to come up with this simon game (schematic and code) we thought we had it but its not working.· we are trying to make a 4 button simon game if anyone has any suggestions on either the schematic or a code that works that would be awesome. thanks for your time.
Comments
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dave Andreae
Parallax Tech Support·
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' "Simon Says..."
'
'
'--- Beep parameters ---
kSpeakerPin CON 15
kDuration CON 100 ' x miliSeconds
kFreq4 CON 659
kFreq3 CON 587
kFreq2 CON 500
kFreq1 CON 440
kPause CON 200
'--- Array parameters ---
kWords CON 0 ' (see below)
kWords1 CON 1 ' kWords1 * 8 = the length of the beep sequence
'--- Game Speed ---
kGameSpeed CON 255 ' Starting game speed
'--- LCD ---
kLCDPin CON 14
N9600 CON $4054
Esc CON 254
CLR CON 1
'--- Variables ---
gArr VAR Word(kWords1) ' Where beep sequence is stored
tRnd VAR Word ' Utility random#
tNote VAR Word ' Frequency of beep
i VAR Byte ' Utility bytes
j VAR Byte '
k VAR Byte '
gWhich VAR Byte ' Which beep to play (0..3)
tBeeps VAR Byte ' How many beeps to play
tWords VAR Byte ' How many words to play
tLoop VAR Byte
gButton VAR Byte
gPause VAR Word
inNum VAR Byte
'
Start:
gPause = kGameSpeed
'--- Clear LCD ---
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,CLR]
PAUSE 500
'--- PIC Says ---
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,128,"Simon Says..."]
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,192,"Press any button"]
'--- Randomize Random# sequence while waiting for button press ---
WaitForStart:
RANDOM tRnd
gButton = INS & %00001111
IF gButton = %00001111 THEN WaitForStart
GOSUB GameStartMusic
'--- Save random numbers (Words) in array gArr() ---
FOR i = 0 TO kWords
RANDOM tRnd
gArr(i) = tRnd
NEXT
'--- Playback the sequence repeatedly, each time playing one more note ---
FOR tBeeps = 0 TO (kWords1 * 8) - 1
GOSUB ShowScore
GOSUB PlaySequence
PAUSE 1000
IF gPause < 20 THEN ItsFastEnough
gPause = gPause - 10
ItsFastEnough:
NEXT
PlayWinSong:
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,CLR]
FREQOUT kSpeakerPin, 100, 900
PAUSE 50
FREQOUT kSpeakerPin, 1000, 900
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,128,"*** BadAss! ***"]
GOTO Start
GameEnd:
FREQOUT kSpeakerPin, 1000, 300
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,CLR]
PAUSE 500
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,128,"** GAME OVER! **"]
GOSUB ShowScore
PAUSE 3000
GOTO Start
'
ShowScore:
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,192,"Score: "]
inNum = tBeeps
GOSUB ShowNum
RETURN
ShowNum:
SEROUT kLCDPin, N9600, [noparse][[/noparse]48 + (inNum / 10)]
SEROUT kLCDPin, N9600, [noparse][[/noparse]48 + (inNum // 10)]
RETURN
inDuration VAR Word
inNote VAR Word
'--- Game Start Music ---
GameStartMusic:
'--- Clear LCD ---
inDuration = 100
inNote = 700
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,CLR]
GOSUB FlashAll
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,128,"Get SET..."]
PAUSE 1000
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,CLR]
GOSUB FlashAll
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,128,"READY..."]
PAUSE 1000
inDuration = 600
SEROUT kLCDPin, N9600, [noparse][[/noparse]Esc,128,"*** PLAY ***"]
GOSUB FlashAll
PAUSE 1000
RETURN
FlashAll:
HIGH 0
HIGH 1
HIGH 2
HIGH 3
FREQOUT kSpeakerPin, inDuration, inNote
LOW 0
LOW 1
LOW 2
LOW 3
RETURN
'--- Playback random numbers two bits at a time ---
' Input: tBeeps = number of beeps to play
PlaySequence:
tWords = tBeeps / 8
FOR tLoop = 0 TO 1 ' 0=play, 1=wait for buttons
FOR i = 0 TO tWords
tRnd = gArr(i)
IF i = tWords THEN LastWord
k = 7
PlayWord:
FOR j = 0 TO k
gWhich = tRnd & 3 ' Get lowest two bits
IF tLoop = 0 THEN JustBeep
GOSUB WaitForButton
IF gWhich <> gButton THEN GameEnd
JustBeep:
GOSUB BeepAndBlink
tRnd = tRnd >> 2 ' Shift down two bits (clobbering the one we've just used)
NEXT
NEXT
NEXT
RETURN
LastWord:
k = tBeeps - (i*8)
GOTO PlayWord
'--- Play tone and blink LED ---
' Input: gWhich (0..3)
BeepAndBlink:
LOOKUP gWhich, [noparse][[/noparse]kFreq1, kFreq2, kFreq3, kFreq4], tNote
HIGH 4 + gWhich
FREQOUT kSpeakerPin, kDuration, tNote
LOW 4 + gWhich
PAUSE gPause
RETURN
'--- Wait for button press ---
WaitForButton:
i = 255
KeepWaiting:
gButton = 0
IF IN0 = 0 THEN Pressed
gButton = 1
IF IN1 = 0 THEN Pressed
gButton = 2
IF IN2 = 0 THEN Pressed
gButton = 3
IF IN3 = 0 THEN Pressed
' Have we waited long enough?
i = i - 1
IF i > 0 THEN KeepWaiting
' If we got here, it means player did not press any button in time.
gButton = -1
Pressed:
RETURN
i've asked my teacher about possible fixes but he has no clue... thanks again
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Dave Andreae
Parallax Tech Support·
www.instructables.com/id/MemAxe-8Bit-sound-effects-memory-game/
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer."
A Google search pulled up nothing, I thought it might have even been posted in the L.O.S.A. (List Of Stamp Applications).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
thanks again
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"If you build it, they will come."