Why is it now working?
mobehun
Posts: 10
Hi!I've made a little game on a BS2SX microcontroller and for some reason the code doesn't seem to work.This is the code:' {$STAMP BS2sx}' {$PBASIC 2.5}btnwrk0 VAR Bytebtnwrk1 VAR Bytebtnwrk2 VAR Bytebtnwrk3 VAR Bytegame VAR Bytenumber VAR Nibjogomb VAR Byteroszgomb VAR Bytebtna PIN 8btnb PIN 9btnc PIN 10btnd PIN 11INPUT 8INPUT 9INPUT 10INPUT 11Start: 'ledek tesztel
Comments
The code requires debugging! Place DEBUG statements within the code so that the program logic can be followed. That will give you a better idea of what's happening. Most likely the code flow is not what you expect.
An alternative to get rand is
seed should be a Word variable.
The 2 Yellow LEDs on P3 seem to be when the wrong button is pressed.
What color are those 2 LEDs next to the BS2SX connected to P2? Those light up when the right button is pressed.
Your code though only lights the P2 LEDs when the number is 4. For every other number it lights the Yellow LEDs.
What color are the P4-P7 LEDs?
Your program has a FOR...NEXT loop of 10, but you end the program if the correct button is pressed 10X.
The player only gets 10 turns?
Also, when the game ends all the LEDs light so how does someone know if they won or lost?
It's also not good practice to use GOTOs, especially if you use FOR...NEXT ad DO...LOOP or looping.
[code]
' {$STAMP BS2sx}
' {$PBASIC 2.5}
'
' P2 --- 470 ohm - (A)LED(C) - VSS
' |_ 470 ohm - (A)LED(C) - VSS
' P3 --- 470 ohm - (A)Yellow_LED(C) - VSS
' |_ 470 ohm - (A)Yellow_LED(C) - VSS
' P4 - 470 ohm - (A)LED(C) - VSS
' P5 - 470 ohm - (A)LED(C) - VSS
' P6 - 470 ohm - (A)LED(C) - VSS
' P7 - 470 ohm - (A)LED(C) - VSS
' P8 --- 220 Ohm - PB - VIN
' |_ 10K - VSS
' P9 --- 220 Ohm - PB - VIN
' |_ 10K - VSS
' P10 --- 220 Ohm - PB - VIN
' |_ 10K - VSS
' P11 --- 220 Ohm - PB - VIN
' |_ 10K - VSS
'
' P4-LED P5-LED P6-LED P7-LED P3-Yellow P3-Yellow P2-LED P2-LED
' P8-PB_A P9-PB_B P10-PB_C P11-PB_D
'
' RED-RED-BRN-GOL (220 ohm, 5%)
' YEL-VIO-BLK-BLK-BRN (470 ohm, 1%)
' BRN-BLK-BLK-RED-BRN (10K, 1%)
btnWrk0 VAR Byte ' Workspace variables for BUTTON command
btnWrk1 VAR Byte
btnWrk2 VAR Byte
btnWrk3 VAR Byte
game VAR Byte ' counter
number VAR Nib ' random numer
joGomb VAR Byte ' good button (Hungarian)
roszGomb VAR Byte ' bad button
randVal VAR Word ' Random number (Added)
counter VAR Nib ' counter variable for end of game blinking (Added)
BtnA PIN 8 ' Pushbuttons
BtnB PIN 9
BtnC PIN 10
BtnD PIN 11
BlinkOn CON %1111 ' Value to write to pin output nibble to turn all LEDs on (Added)
BlinkOff CON %0000 ' Value to write to pin output nibble to turn all LEDs off (Added)
' Initialization
' All pins are inputs when BS2 is powered on.
'INPUT 8
'INPUT 9
'INPUT 10
'INPUT 11
' Set pin direction registers for LEDs to output
' Note: Direction registers use nibble format (4-bits)
DIRA = %1100 ' Set P3 and P2 to be outputs, but leave P1 and P0 as inputs. (Added)
DIRB = %1111 ' Set P7-P4 to be outputs. (Added)
Start: 'ledek tesztel
When testing a specific function, like a detecting a button, it is a good idea to create a test harness designed for the specific function. Wire up a single button circuit and code. The BS2 manual has excellent examples.
@Sapphire, thanks
Ending part:
Thank you for the code. It helped me understand lots of things. For some reason even if I don't push any button it jumps to the J_Gb part 2 times and then to the Lose part.
I think you should make those 2 LEDs by your BS2SX Green since they light when the correct button is pressed, and change the 2 Yellow LEDs to Red.
Your switches are not wired up correctly. The 10K resistors are connected to the I/O pins and to ground, so the I/O pin will always see ground and remain at 0.
http://www.parallax.com/downloads/whats-microcontroller-text
Look at page 69 of v3.0
This is how an Active-Low pushbutton should be wired. Notice how the I/O pin always sees +V, but when the switch is pressed it sees ground.
Also, according to Parallax diagrams, switches and LEDs are connected to VDD which is 5V. What voltage do you have them connected to.
The module regulator only provides 50 mA and the Stamp itself needs about half of that.
And how do you restart your program because there isn't a Reset button. I also don't see wiring for programming.
You can save yourself a lot of trouble by using a BS2-OEM. It has 20 pins in a line, a built-in regulator up to 1A (I think), and a built-in programming port.
Your program would also be a lot simpler if you didn't use the BUTTON command.
Most people don't use the BUTTON command, but use INx instead.
Page 72 of What's a Microcontroller (WAM) is an example of blinking an LED with a pushbutton. (This button is wired the opposite of yours)
[code]
' {$STAMP BS2sx}
' {$PBASIC 2.5}
btnWrk0 VAR Byte ' Workspace variables for BUTTON command
btnWrk1 VAR Byte
btnWrk2 VAR Byte
btnWrk3 VAR Byte
game VAR Byte ' counter
number VAR Nib ' random numer
joGomb VAR Byte ' good button (Hungarian)
roszGomb VAR Byte ' bad button
randVal VAR Word ' Random number (Added)
counter VAR Nib ' counter variable for end of game blinking (Added)
BtnA PIN 8 ' Pushbuttons
BtnB PIN 9
BtnC PIN 10
BtnD PIN 11
BlinkOn CON %1111 ' Value to write to pin output nibble to turn all LEDs on (Added)
BlinkOff CON %0000 ' Value to write to pin output nibble to turn all LEDs off (Added)
' Initialization
' All pins are inputs when BS2 is powered on.
'INPUT 8
'INPUT 9
'INPUT 10
'INPUT 11
' Set pin direction registers for LEDs to output
' Note: Direction registers use nibble format (4-bits)
DIRA = %1100 ' Set P3 and P2 to be outputs, but leave P1 and P0 as inputs. (Added)
DIRB = %1111 ' Set P7-P4 to be outputs. (Added)
Start: 'ledek tesztel
Does the player only get 10 tries or does the player get as many tries as they need to win or lose.
You could have the program light up the LEDs for the number of turns the player took. There are 4 LEDs so you can display a number from 0 to 15.
Well, the player could be wrong 5 times and still win in 15 turns.....was this intentional or by design?
To use Reset, connect an Active-Low switch to the /RES pin on the BS2. The bar means this pin is active-low or that it works when it sees a 0 which is ground (VSS).
It's very important that tie this pin to VDD through a resistor so it doesn't get triggered by accident.
Pressing the Reset button connects the /RES pin ground and causes the BS2 to reset.
Without a Reset button the only way to restart the program is by disconnecting and reconnecting power to the BS2.
At the end of the game, why not leave the Win or Lose LEDs lit instead of flashing them.