Basic Stamp- adding music during the LED game
cynla
Posts: 2
Hi!
I have a problem. I found this code on your website for Bi-Colour LED game and I was successful. However, I am trying to add music while the game is running but am unable to figure out where to put the code for the music. Here is the Code:
' BiColorLedGame.bs2
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ EEPROM Definitions ]
eepromAddress VAR Word ' Current address in EEPROM
currentAddress VAR Word ' Address counter variable for
' reading from EEPROM
'
[ I/O Definitions ]
displayRed PIN 15 ' Red Pin
displayGreen PIN 14 ' Green Pin
'
[ Variables ]
PbGreen VAR Byte ' Pushbutton pressed = green
PbRed VAR Byte ' Pushbutton pressed = red
Green VAR Byte ' Green pushbutton select
Red VAR Byte ' Red pushbutton select
btnWrk VAR Byte ' BUTTON command workspace
youLose VAR Byte ' Lose flag
roundNumber VAR Word ' Current level
delayTime VAR Word ' Current light delay time
generateColor VAR Word ' Random color generated
displayColor VAR generateColor.BIT0 ' Bit 0 of random color gen.
savedColor VAR Word ' Save colors displayed
GOSUB Start_Game ' At startup, go to the
' Begin_Game subroutine
'
[ Main Routine ]
Main:
DO
GOSUB Verify_Response ' Check player response
IF (youLose = 1) THEN ' If the youLose flag has been
DEBUG "You got it WRONG! You LOST. New Game.", CR, ' set, inform the player, & show
"You got to Level: ", ' the highest level reached
DEC roundNumber
PAUSE 2000
DEBUG CLS
GOSUB Start_Game ' Reset - go to Begin_Game
ELSEIF (youLose = 0) THEN ' If the youLose flag was not
GOSUB Next_Round ' set, go to the Next_Round
ENDIF ' subroutine
LOOP
' [Song]
Notes DATA "C", "F", "C", "f", "C", "F", "C",
"C", "F", "C", "F", "A", "G", "F", "E", "D", "D",
"C", "F", "C", "f", "C", "F", "C",
"F", "D", "C", "B", "A", "G", "f", "S"
Durations DATA 4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
4, 4, 4, 4, 4, 4, 2,
4, 8, 4, 4, 4, 4, 4
Dots DATA 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
WholeNote CON 2000
index VAR Byte
offset VAR Nib
noteLetter VAR Byte
noteFreq VAR Word
noteDuration VAR Word
DO UNTIL noteLetter = "S"
READ Notes + index, noteLetter
LOOKDOWN noteLetter, [ "A", "a", "B", "C", "D", "d", "E", "F", "f", "G", "S" ], offset
LOOKUP offset, [ 3520, 1760, 1976, 2349, 1218, 2637, 2793, 1397, 3136, 0], noteFreq
READ Durations + index, noteDuration
noteDuration = WholeNote / noteDuration
FREQOUT 6, noteDuration, noteFreq
index = index + 1
LOOP
'
[ Subroutine - Start_Game ]
Start_Game:
' Reset all variables to their initial values.
eepromAddress = 0
currentAddress = 0
roundNumber = 0
delayTime = 750
DEBUG HOME, "Start by Pressing the Pushbutton!", CR
RANDOM generateColor
BUTTON 0, 1, 100, 250, btnWrk, 0, Start_Game
DEBUG CLS
IF (displayColor = 1) THEN ' If bit0 of the random number
HIGH displayRed ' generated is 1, then display
LOW displayGreen ' the color red.
PAUSE delayTime ' Wait for the value in delayTime
WRITE eepromAddress, 1 ' Write a 1 to EEPROM to save
' the color displayed
ELSEIF (displayColor = 0) THEN ' If bit0 of the random number
HIGH displayGreen ' generated is 0, then display
LOW displayRed ' the color green.
PAUSE delayTime ' Wait for the value in delayTime
WRITE eepromAddress, 0 ' Write a 0 to EEPROM to save
ENDIF ' the color displayed
LOW displayRed
LOW displayGreen
RETURN
'
[ Subroutine - Verify_Response ]
Verify_Response:
youLose = 0
FOR eepromAddress = 0 TO currentAddress
READ eepromAddress, savedColor
' Monitor pushbutton states until one is pressed
DO
PbGreen = IN1 ' State of pin 1 (green button)
PbRed = IN0 ' State of pin 0 (red button)
LOOP UNTIL (PbGreen = 1) OR (PbRed = 1)
Green = PbGreen ' Save the state of Pushbuttons
Red = PbRed
' Wait until the player releases the pushbutton
DO
PbGreen = IN1 ' State of pin 1 (green button)
PbRed = IN0 ' State of pin 0 (red button)
LOOP UNTIL (PbGreen = 0) AND (PbRed = 0)
IF savedColor <> Green THEN ' If the values of savedColor
PbGreen = 0 ' and Green are different,
PAUSE 100 ' keep looping
ELSEIF savedColor <> Red THEN ' If the values of savedColor
youLose = 1 ' and Red are different, the
RETURN ' player lost, return to main
ELSEIF savedColor = Red THEN ' If the values of savedColor
PbRed = 0 ' and Red are equal, keep
PAUSE 100 ' looping
ELSEIF savedColor = Green THEN ' If the values of savedColor
youLose = 1 ' and Red are equal, the
RETURN ' player lost, return to main
ENDIF
NEXT
' +1 for the round number, player is still playing
roundNumber = roundNumber + 1
RETURN
'
[ Subroutine - Check_Response ]
' Shows the last colour(s) and adds a new random colour to the sequence.
Next_Round:
PAUSE 750
delayTime = (delayTime - 50) MIN 200 ' Makes the delay time shorter but not nonexistent
FOR eepromAddress = 0 TO currentAddress
READ eepromAddress, savedColor
IF (savedColor = 1) THEN
HIGH displayRed
LOW displayGreen
PAUSE delayTime
ELSEIF (savedColor = 0) THEN
HIGH displayGreen
LOW displayRed
PAUSE delayTime
ENDIF
LOW displayRed
LOW displayGreen
PAUSE delayTime
NEXT
RANDOM generateColor
IF (displayColor = 1) THEN
HIGH displayRed
LOW displayGreen
PAUSE delayTime
WRITE eepromAddress, 1
ELSEIF (displayColor = 0) THEN
HIGH displayGreen
LOW displayRed
PAUSE delayTime
WRITE eepromAddress, 0
ENDIF
LOW displayRed
LOW displayGreen
currentAddress = currentAddress + 1
RETURN
I have a problem. I found this code on your website for Bi-Colour LED game and I was successful. However, I am trying to add music while the game is running but am unable to figure out where to put the code for the music. Here is the Code:
' BiColorLedGame.bs2
' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ EEPROM Definitions ]
eepromAddress VAR Word ' Current address in EEPROM
currentAddress VAR Word ' Address counter variable for
' reading from EEPROM
'
[ I/O Definitions ]
displayRed PIN 15 ' Red Pin
displayGreen PIN 14 ' Green Pin
'
[ Variables ]
PbGreen VAR Byte ' Pushbutton pressed = green
PbRed VAR Byte ' Pushbutton pressed = red
Green VAR Byte ' Green pushbutton select
Red VAR Byte ' Red pushbutton select
btnWrk VAR Byte ' BUTTON command workspace
youLose VAR Byte ' Lose flag
roundNumber VAR Word ' Current level
delayTime VAR Word ' Current light delay time
generateColor VAR Word ' Random color generated
displayColor VAR generateColor.BIT0 ' Bit 0 of random color gen.
savedColor VAR Word ' Save colors displayed
GOSUB Start_Game ' At startup, go to the
' Begin_Game subroutine
'
[ Main Routine ]
Main:
DO
GOSUB Verify_Response ' Check player response
IF (youLose = 1) THEN ' If the youLose flag has been
DEBUG "You got it WRONG! You LOST. New Game.", CR, ' set, inform the player, & show
"You got to Level: ", ' the highest level reached
DEC roundNumber
PAUSE 2000
DEBUG CLS
GOSUB Start_Game ' Reset - go to Begin_Game
ELSEIF (youLose = 0) THEN ' If the youLose flag was not
GOSUB Next_Round ' set, go to the Next_Round
ENDIF ' subroutine
LOOP
' [Song]
Notes DATA "C", "F", "C", "f", "C", "F", "C",
"C", "F", "C", "F", "A", "G", "F", "E", "D", "D",
"C", "F", "C", "f", "C", "F", "C",
"F", "D", "C", "B", "A", "G", "f", "S"
Durations DATA 4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
4, 4, 4, 4, 4, 4, 2,
4, 8, 4, 4, 4, 4, 4
Dots DATA 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0
WholeNote CON 2000
index VAR Byte
offset VAR Nib
noteLetter VAR Byte
noteFreq VAR Word
noteDuration VAR Word
DO UNTIL noteLetter = "S"
READ Notes + index, noteLetter
LOOKDOWN noteLetter, [ "A", "a", "B", "C", "D", "d", "E", "F", "f", "G", "S" ], offset
LOOKUP offset, [ 3520, 1760, 1976, 2349, 1218, 2637, 2793, 1397, 3136, 0], noteFreq
READ Durations + index, noteDuration
noteDuration = WholeNote / noteDuration
FREQOUT 6, noteDuration, noteFreq
index = index + 1
LOOP
'
[ Subroutine - Start_Game ]
Start_Game:
' Reset all variables to their initial values.
eepromAddress = 0
currentAddress = 0
roundNumber = 0
delayTime = 750
DEBUG HOME, "Start by Pressing the Pushbutton!", CR
RANDOM generateColor
BUTTON 0, 1, 100, 250, btnWrk, 0, Start_Game
DEBUG CLS
IF (displayColor = 1) THEN ' If bit0 of the random number
HIGH displayRed ' generated is 1, then display
LOW displayGreen ' the color red.
PAUSE delayTime ' Wait for the value in delayTime
WRITE eepromAddress, 1 ' Write a 1 to EEPROM to save
' the color displayed
ELSEIF (displayColor = 0) THEN ' If bit0 of the random number
HIGH displayGreen ' generated is 0, then display
LOW displayRed ' the color green.
PAUSE delayTime ' Wait for the value in delayTime
WRITE eepromAddress, 0 ' Write a 0 to EEPROM to save
ENDIF ' the color displayed
LOW displayRed
LOW displayGreen
RETURN
'
[ Subroutine - Verify_Response ]
Verify_Response:
youLose = 0
FOR eepromAddress = 0 TO currentAddress
READ eepromAddress, savedColor
' Monitor pushbutton states until one is pressed
DO
PbGreen = IN1 ' State of pin 1 (green button)
PbRed = IN0 ' State of pin 0 (red button)
LOOP UNTIL (PbGreen = 1) OR (PbRed = 1)
Green = PbGreen ' Save the state of Pushbuttons
Red = PbRed
' Wait until the player releases the pushbutton
DO
PbGreen = IN1 ' State of pin 1 (green button)
PbRed = IN0 ' State of pin 0 (red button)
LOOP UNTIL (PbGreen = 0) AND (PbRed = 0)
IF savedColor <> Green THEN ' If the values of savedColor
PbGreen = 0 ' and Green are different,
PAUSE 100 ' keep looping
ELSEIF savedColor <> Red THEN ' If the values of savedColor
youLose = 1 ' and Red are different, the
RETURN ' player lost, return to main
ELSEIF savedColor = Red THEN ' If the values of savedColor
PbRed = 0 ' and Red are equal, keep
PAUSE 100 ' looping
ELSEIF savedColor = Green THEN ' If the values of savedColor
youLose = 1 ' and Red are equal, the
RETURN ' player lost, return to main
ENDIF
NEXT
' +1 for the round number, player is still playing
roundNumber = roundNumber + 1
RETURN
'
[ Subroutine - Check_Response ]
' Shows the last colour(s) and adds a new random colour to the sequence.
Next_Round:
PAUSE 750
delayTime = (delayTime - 50) MIN 200 ' Makes the delay time shorter but not nonexistent
FOR eepromAddress = 0 TO currentAddress
READ eepromAddress, savedColor
IF (savedColor = 1) THEN
HIGH displayRed
LOW displayGreen
PAUSE delayTime
ELSEIF (savedColor = 0) THEN
HIGH displayGreen
LOW displayRed
PAUSE delayTime
ENDIF
LOW displayRed
LOW displayGreen
PAUSE delayTime
NEXT
RANDOM generateColor
IF (displayColor = 1) THEN
HIGH displayRed
LOW displayGreen
PAUSE delayTime
WRITE eepromAddress, 1
ELSEIF (displayColor = 0) THEN
HIGH displayGreen
LOW displayRed
PAUSE delayTime
WRITE eepromAddress, 0
ENDIF
LOW displayRed
LOW displayGreen
currentAddress = currentAddress + 1
RETURN
Comments
A workaround is to replace the pauses in your program with tones, or at least beeps. "Music" is in the ear of the beholder.