Help with coding an LED puzzle game ?
kidsasuke
Posts: 8
Hi everyone,
I'm new to using the BASIC Stamp discovery kit and am not THAT familiar with proper syntax yet. I was looking to design a basic puzzle game where you're trying to turn all 7 LEDs in a line off without having them all turn on. The plan is that whenever you change the state of any LED, the adjacent ones will change states (which is probably going to be my biggest hurdle if possible at all with this kit).
So far, I've been trying to simply code a piezo speaker to sound either a failure or victory sound depending on the outcome of the game (all on or all off). I assumed that the following code would accomplish this (with the series of high/low statements at the bottom for testing whether or not the code worked)
' {$STAMP BS2}
' {$PBASIC 2.5}
' Creating a binary puzzle game with two pushbuttons/possibly debug inputs......probably debug inputs....but hey, who knows?
' Object of the game is to make all of the lights turn off without making them all turn on. When one light is turned on the
' adjacent lights will change states. There will be a total of 7 LEDs and they will start in a random pattern (hopefully).
' Pins 0 - 6 will be the LEDs and a piezo speaker (pin15) will make a fail sound upon losing, or a victory fanfare upon winning!
' Start by creating the victory and failure sounds.
state VAR Bit
counter VAR Word
IF (IN0 AND IN1 AND IN2 AND IN3 AND IN4 AND IN5 AND IN6 = 1) THEN
FOR counter = 1 TO 10
HIGH 15
PAUSE counter
LOW 15
PAUSE counter
NEXT
FOR counter = 1 TO 20
HIGH 15
PAUSE counter
LOW 15
PAUSE counter
NEXT
FOR counter = 1 TO 40
HIGH 15
PAUSE counter
LOW 15
PAUSE counter
NEXT
ENDIF
HIGH 0
LOW 1
HIGH 2
HIGH 3
HIGH 4
HIGH 5
HIGH 6
The sound comes out perfectly fine, but the code seems ambiguous about when the piezo will sound. It'll ALWAYS fire off when all pins are high, but then randomly fire off in any other state (sometimes it won't sound when the program is run, but then hitting the reset button on the board will cause it to sound. Other times, it'll fully take the code and won't make a sound even when reset.)
Can anybody help me out with this issue? If needed, I can pull together a list of the exact pin states that cause the different levels of ambiguity. This may actually become my final project for class, so any contributors with this work will definitely be cited for their help.
Many thanks in advance for any advice.
I'm new to using the BASIC Stamp discovery kit and am not THAT familiar with proper syntax yet. I was looking to design a basic puzzle game where you're trying to turn all 7 LEDs in a line off without having them all turn on. The plan is that whenever you change the state of any LED, the adjacent ones will change states (which is probably going to be my biggest hurdle if possible at all with this kit).
So far, I've been trying to simply code a piezo speaker to sound either a failure or victory sound depending on the outcome of the game (all on or all off). I assumed that the following code would accomplish this (with the series of high/low statements at the bottom for testing whether or not the code worked)
' {$STAMP BS2}
' {$PBASIC 2.5}
' Creating a binary puzzle game with two pushbuttons/possibly debug inputs......probably debug inputs....but hey, who knows?
' Object of the game is to make all of the lights turn off without making them all turn on. When one light is turned on the
' adjacent lights will change states. There will be a total of 7 LEDs and they will start in a random pattern (hopefully).
' Pins 0 - 6 will be the LEDs and a piezo speaker (pin15) will make a fail sound upon losing, or a victory fanfare upon winning!
' Start by creating the victory and failure sounds.
state VAR Bit
counter VAR Word
IF (IN0 AND IN1 AND IN2 AND IN3 AND IN4 AND IN5 AND IN6 = 1) THEN
FOR counter = 1 TO 10
HIGH 15
PAUSE counter
LOW 15
PAUSE counter
NEXT
FOR counter = 1 TO 20
HIGH 15
PAUSE counter
LOW 15
PAUSE counter
NEXT
FOR counter = 1 TO 40
HIGH 15
PAUSE counter
LOW 15
PAUSE counter
NEXT
ENDIF
HIGH 0
LOW 1
HIGH 2
HIGH 3
HIGH 4
HIGH 5
HIGH 6
The sound comes out perfectly fine, but the code seems ambiguous about when the piezo will sound. It'll ALWAYS fire off when all pins are high, but then randomly fire off in any other state (sometimes it won't sound when the program is run, but then hitting the reset button on the board will cause it to sound. Other times, it'll fully take the code and won't make a sound even when reset.)
Can anybody help me out with this issue? If needed, I can pull together a list of the exact pin states that cause the different levels of ambiguity. This may actually become my final project for class, so any contributors with this work will definitely be cited for their help.
Many thanks in advance for any advice.
Comments
Thanks again for your help.
light var nib 'This is the light that was "pressed"
lights var bit(7)
Toggle_Lights:
if (light = 0) then
lights(1) = lights(1) + 1
elseif(light = 7) then
lights(6) = lights(6) + 1
else
lights(light + 1) = lights(light + 1) + 1
lights(light - 1) = lights(light - 1) + 1
endif
light = light + 1