Shop OBEX P1 Docs P2 Docs Learn Events
Why is it now working? — Parallax Forums

Why is it now working?

mobehunmobehun Posts: 10
edited 2014-06-05 15:17 in General Discussion
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

  • mobehunmobehun Posts: 10
    edited 2014-06-02 01:52
    Hi!I've made a little game on a BS2SX microcontroller and for some reason the code doesn't seem to work. Pastebin link to the code: http://pastebin.com/6iJnp8px Photo of my setup: http://i.imgur.com/BKgsZOi.jpg
  • TCTC Posts: 1,019
    edited 2014-06-02 03:19
    mobehun wrote: »
    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
  • Mike GMike G Posts: 2,702
    edited 2014-06-02 03:40
    The RANDOM command in Main's FOR...NEXT loop generates a number between 0 and 65535. The program logic only looks for 4,5,6 or 7. If 4,5,6 ,or 7 are not detected the program jumps to Rgb which increments a variable. The variable is checked against the number 6 where either the program ends or jumps to Main and starts the FOR...NEXT loop again.

    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.
  • mobehunmobehun Posts: 10
    edited 2014-06-02 05:58
    Thanks for the help. I'll try it out.
  • mobehunmobehun Posts: 10
    edited 2014-06-03 09:11
    What should I use instead of RANDOM if I only want to use those numbers in a random order?
  • Mike GMike G Posts: 2,702
    edited 2014-06-03 13:24
    We can try a little logic and math. I believe this will work. It's not tested and it might not appear totally random.
      RANDOM rand
      'Mask bits 0 and 1.  This will allow only numbers 0,1,2, and 3
      'Add 4 to get to 4,5,6 and 7
      rand = (rand & %0011) + 4
    
  • SapphireSapphire Posts: 496
    edited 2014-06-03 13:52
    It will not be random, because this alters the seed. To do this, you must use another variable for the seed and never touch it.
      RANDOM seed
      'Mask bits 0 and 1.  This will allow only numbers 0,1,2, and 3
      'Add 4 to get to 4,5,6 and 7
      rand = (seed & %0011) + 4
    

    An alternative to get rand is
      rand = (seed // 4) + 4
    

    seed should be a Word variable.
  • mobehunmobehun Posts: 10
    edited 2014-06-04 01:56
    Thanks for the help with the RANDOM thing. One other thing is that the program I wrote doesn't seem to detect if I push a button or not. Is it because the IF...THEN parts?

    Sapphire wrote: »
    It will not be random, because this alters the seed. To do this, you must use another variable for the seed and never touch it.
      RANDOM seed
      'Mask bits 0 and 1.  This will allow only numbers 0,1,2, and 3
      'Add 4 to get to 4,5,6 and 7
      rand = (seed & 11) + 4
    

    An alternative to get rand is
      rand = (seed // 4) + 4
    

    seed should be a Word variable.
  • GenetixGenetix Posts: 1,754
    edited 2014-06-04 02:22
    It took me a while to figure out how your circuit is wired and I still don't understand how your program works.
    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
  • Mike GMike G Posts: 2,702
    edited 2014-06-04 03:36
    Thanks for the help with the RANDOM thing. One other thing is that the program I wrote doesn't seem to detect if I push a button or not. Is it because the IF...THEN parts?
    As suggested in post 4, debug your code! Place DEBUG statements within the code project to determine the actual code flow. See the BS2 manual for DEBUG ideas.

    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
  • mobehunmobehun Posts: 10
    edited 2014-06-04 06:00
    I know my work looks ugly as hell and I'm sorry about that. I used 2 yellow LEDs and 6 white LEDs. I fixed the buttons in the program and the ending part as well.
    Ending part:
    Win:
    HIGH 2
    HIGH 4
    HIGH 5
    HIGH 6
    HIGH 7
    PAUSE 1000
    LOW 2
    LOW 4
    LOW 5
    LOW 6
    LOW 7
    END
    
    
    Lose:
    HIGH 3
    HIGH 4
    HIGH 5
    HIGH 6
    HIGH 7
    PAUSE 1000
    LOW 3
    LOW 4
    LOW 5
    LOW 6
    LOW 7
    END
    

    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.
  • GenetixGenetix Posts: 1,754
    edited 2014-06-04 09:53
    Can you please post your full program.
    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)
  • mobehunmobehun Posts: 10
    edited 2014-06-05 02:39
    This is the program I'm using right now( Thanks Genetix :D ):
    [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
  • GenetixGenetix Posts: 1,754
    edited 2014-06-05 15:17
    You answered most of my questions but I still don't understand how that FOR...NEXT (1 TO 10) loop relates to winning or losing.
    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.
Sign In or Register to comment.