Shop OBEX P1 Docs P2 Docs Learn Events
BS1 Help — Parallax Forums

BS1 Help

gilliganivgilliganiv Posts: 3
edited 2005-04-27 17:31 in Learn with BlocklyProp
Hi all,
I'm working on a design project for a class using the BS1 and I'm having trouble writing the code.· Our design has seven buttons and seven LED's (one for each button) that are connected in parallel with the button.· We would like a LED to illuminate when·a button is pressed and to stay illuminated until the next button is pressed.· So at any given time, only one LED is illuminated and that LED corresponds to the button that has been pressed.··Any help·is appreciated, as I am very new to BASIC programming and to the BS1.· The code that I am using is as follows:

'{$STAMP BS1}
'{$PBASIC 1.0}

DIRS = %10000000

Main:
IF DIR1 = 0 THEN Button1
Main2:
IF DIR2 = 0 THEN Button2
Main3:
IF DIR3 = 0 THEN Button3
Main4:
IF DIR4 = 0 THEN Button4
Main5:
IF DIR5 = 0 THEN Button5
Main6:
IF DIR6 = 0 THEN Button6
Main7:
IF DIR7 = 0 THEN Button7
GOTO Main

Button1:
· IF PIN1 = 1 THEN Press1
· GOTO Main2
Button2:
· IF PIN2 = 1 THEN Press2
· GOTO Main3
Button3:
· IF PIN3 = 1 THEN Press3
· GOTO Main4
BUTTON4:
· IF PIN4 = 1 THEN Press4
· GOTO Main5
Button5:
· IF PIN5 =1 THEN Press5
· GOTO Main6
BUTTON6:
· IF PIN6 = 1 THEN Press6
· GOTO Main7
Button7:
· IF PIN7 = 1 THEN Press7
· GOTO Main

Press1:
DIRS = %11000000
HIGH 1
GOTO Main

Press2:
DIRS = %10100000
HIGH 2
GOTO Main

Press3:
DIRS = %10010000
HIGH 3
GOTO Main

Press4:
DIRS = %10001000
HIGH 4
GOTO Main

Press5:
DIRS = %10000100
HIGH 5
GOTO Main

Press6:
DIRS = %10000010
HIGH 6
GOTO Main

Press7:
DIRS = %10000001
HIGH 7
GOTO Main


Thanks for your help.

Comments

  • GadgetmanGadgetman Posts: 2,436
    edited 2005-04-26 15:19
    You're playing with fire...
    Using pins as both input nd output is not easy.

    Another way of doing it is to use Shift-Registers.
    Connect the buttons to one parallell-in/serial-out type register, and the LEDs to a serial-in/parallel-out type register.
    (See articles on expanding number or pins among the Nuts&Volts pages)

    Then just SHIFTIN the button status, SHIFTOUT the same Byte to the LEDs, take a PAUSE and GOTO start...

    You may want to examine the status Byte, though, particularly if it only allows for one LED to light at any time.

    Have fun!
  • gilliganivgilliganiv Posts: 3
    edited 2005-04-27 12:07
    My first code definitely didnt work.· I rewrote it and now 6 of the 7 buttons are completely functional.· Any ideas why Pin7 may not be working?· Thanks.· My new code is as follows:

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    SYMBOL Ledstate = B1
    Ledstate = %10000000
    SYMBOL x = B3

    Main:
    DIRS = %11111111
    Chk1:
    x = Ledstate & %01000000
    IF x = 0 THEN Off1
    LOW 1
    GOTO Chk2
    Off1:
    HIGH 1

    Chk2:
    x = Ledstate & %00100000
    IF x = 0 THEN Off2
    LOW 2
    GOTO Chk3
    Off2:
    HIGH 2

    Chk3:
    x = Ledstate & %00010000
    IF x = 0 THEN Off3
    LOW 3
    GOTO Chk4
    Off3:
    HIGH 3

    Chk4:
    x = Ledstate & %00001000
    IF x = 0 THEN Off4
    LOW 4
    GOTO Chk5
    Off4:
    HIGH 4

    Chk5:
    x = Ledstate & %00000100
    IF x = 0 THEN Off5
    LOW 5
    GOTO Chk6
    Off5:
    HIGH 5

    Chk6:
    x = Ledstate & %00000010
    IF x = 0 THEN Off6
    LOW 6
    GOTO Chk7
    Off6:
    HIGH 6

    Chk7:
    x = Ledstate & %00000001
    IF x = 0 THEN Off7
    LOW 7
    GOTO Wait
    Off7:
    HIGH 7

    Wait:
    DIRS = %10000000

    CheckInputs:
    IF PIN1 = 0 THEN Update1
    GOTO ChkP2
    Update1:
    Ledstate = %11000000
    GOTO ChkDne

    ChkP2:
    IF PIN2 = 0 THEN Update2
    GOTO ChkP3
    Update2:
    Ledstate = %10100000
    GOTO ChkDne

    ChkP3:
    IF PIN3 = 0 THEN Update3
    GOTO ChkP4
    Update3:
    Ledstate = %10010000
    GOTO ChkDne

    ChkP4:
    IF PIN4 = 0 THEN Update4
    GOTO ChkP5
    Update4:
    Ledstate = %10001000
    GOTO ChkDne

    ChkP5:
    IF PIN5 = 0 THEN Update5
    GOTO ChkP6
    Update5:
    Ledstate = %10000100
    GOTO ChkDne

    ChkP6:
    IF PIN6 = 0 THEN Update6
    GOTO ChkP7
    Update6:
    Ledstate = %10000010
    GOTO ChkDne

    ChkP7:
    IF PIN7 = 0 THEN Update7
    GOTO ChkDne
    Update7:
    Ledstate = %10000001
    GOTO ChkDne

    ChkDne:
    GOTO Main
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-04-27 13:31
    Can you post a schematic of your button/LED circuit -- that would help suss out the code.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • gilliganivgilliganiv Posts: 3
    edited 2005-04-27 17:31
    I uploaded a .jpg file that shows the circuit we are using for the button/LED.· Thanks for your help!
Sign In or Register to comment.