Shop OBEX P1 Docs P2 Docs Learn Events
Basic I/O problem — Parallax Forums

Basic I/O problem

ArchiverArchiver Posts: 46,084
edited 2003-09-04 13:00 in General Discussion
I am very new to writing code for the basic stamp. I am trying to do
a simple 2 in 2 out using the BS2. I wrote the code listed below and
each output (14 & 15) will flash, but if both inputs (buttons)are
pressed at the same time the outputs follow each other. I understand
that the program runs from top down, but is there a way to allow each
input to control it's specified output regardless of where it is in
the program and independently? Am I way off base with
the "IF" "THEN"?


' {$STAMP BS2}
' {$PBASIC 2.5}

DO

IF (IN3 = 1) THEN
HIGH 14
PAUSE 500
LOW 14
PAUSE 500
ELSEIF (IN3 = 0) THEN
LOW 14
ENDIF
IF (IN4 = 1)THEN
HIGH 15
PAUSE 50
LOW 15
PAUSE 50
ELSEIF (IN4 = 0)THEN
LOW 15
ENDIF
IF (IN5 = 1) THEN
HIGH 13
PAUSE 500
LOW 13
PAUSE 50
ELSEIF (IN5 = 0) THEN
LOW 13
ENDIF
PAUSE 50
LOOP

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-09-04 01:54
    It is possible to refer to pins 12, 13, 14, 15 as
    OUTD, or IND (thus reading 4 bits at a time,
    and setting 4 bits at a time).

    'A' is 0..3, 'B' is 4..7, 'C' is 8..11,
    and 'D' is 12..15.

    So, if you say:
    DIRA = %0000 ' Make pins 0..3 inputs
    DIRD = %1100 ' Make pins 15, 14 as outputs
    MyVar VAR NIB

    MainLoop:
    MyVar = INA
    OUTD = MyVar
    GOTO LOOP

    ' Then, you'll read 4 pins at a time,
    ' (only really using 2) and write
    ' 4 pins at a time (but only 2 of them
    ' will be outputs.


    --- In basicstamps@yahoogroups.com, "voltsguy" <brianr@d...> wrote:
    > I am very new to writing code for the basic stamp. I am trying to
    do
    > a simple 2 in 2 out using the BS2. I wrote the code listed below
    and
    > each output (14 & 15) will flash, but if both inputs (buttons)are
    > pressed at the same time the outputs follow each other. I
    understand
    > that the program runs from top down, but is there a way to allow
    each
    > input to control it's specified output regardless of where it is in
    > the program and independently? Am I way off base with
    > the "IF" "THEN"?
    >
    >
    > ' {$STAMP BS2}
    > ' {$PBASIC 2.5}
    >
    > DO
    >
    > IF (IN3 = 1) THEN
    > HIGH 14
    > PAUSE 500
    > LOW 14
    > PAUSE 500
    > ELSEIF (IN3 = 0) THEN
    > LOW 14
    > ENDIF
    > IF (IN4 = 1)THEN
    > HIGH 15
    > PAUSE 50
    > LOW 15
    > PAUSE 50
    > ELSEIF (IN4 = 0)THEN
    > LOW 15
    > ENDIF
    > IF (IN5 = 1) THEN
    > HIGH 13
    > PAUSE 500
    > LOW 13
    > PAUSE 50
    > ELSEIF (IN5 = 0) THEN
    > LOW 13
    > ENDIF
    > PAUSE 50
    > LOOP
  • ArchiverArchiver Posts: 46,084
    edited 2003-09-04 02:39
    You can actually simplify your code. You don't need the ELSEIF
    construct because your test variables (inputs) have only two states.
    IF-THEN-ELSEIF is appropriate when your test variable can have more than
    two states or you are testing more than one variable. And in truth,
    your code doesn't even need the ELSE clause because the THEN portion
    always leaves the LED off when it's finished (which is what your ELSE
    section was doing).

    Corrected, here's what on chunk of your loop would look like (Note that
    I have added code to make things more readable and easier to update):

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    BtnA PIN 3
    LedA PIN 14

    Pressed CON 1

    LedDelay CON 500
    LoopDelay CON 50


    Main:
    DO
    IF (BtnA = Pressed) THEN
    HIGH LedA
    PAUSE LedDelay
    LOW LedA
    PAUSE LedDelay
    ENDIF

    ' other buttons / tests

    PAUSE LoopDelay
    LOOP

    As Allan pointed out in his post, you can use parallel I/O groups to
    prevent the inter-LED delays caused by sequential code.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    Btns VAR INA
    Leds VAR OUTD

    LedDelay CON 500
    LoopDelay CON 50

    Setup:
    Leds = %0000
    DIRD = %1111

    Main:
    DO
    Leds = Btns
    PAUSE LedDelay
    Leds = %0000
    PAUSE LedDelay
    LOOP

    Notice that this code requires a bit of setup for the LEDs as the
    program is not using HIGH and LOW which take care of making the
    specified pin an output.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: voltsguy [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=n1yIM-8yEO6DNte2xw6YqVNLmQd6Cu_tZ8ZrWDnVqMYQNv6Eg90bbnqA_9lCMX1KHee12f3e6G8c1Bul]brianr@d...[/url
    Sent: Wednesday, September 03, 2003 7:15 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Basic I/O problem


    I am very new to writing code for the basic stamp. I am trying to do
    a simple 2 in 2 out using the BS2. I wrote the code listed below and
    each output (14 & 15) will flash, but if both inputs (buttons)are
    pressed at the same time the outputs follow each other. I understand
    that the program runs from top down, but is there a way to allow each
    input to control it's specified output regardless of where it is in
    the program and independently? Am I way off base with
    the "IF" "THEN"?


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    DO

    IF (IN3 = 1) THEN
    HIGH 14
    PAUSE 500
    LOW 14
    PAUSE 500
    ELSEIF (IN3 = 0) THEN
    LOW 14
    ENDIF
    IF (IN4 = 1)THEN
    HIGH 15
    PAUSE 50
    LOW 15
    PAUSE 50
    ELSEIF (IN4 = 0)THEN
    LOW 15
    ENDIF
    IF (IN5 = 1) THEN
    HIGH 13
    PAUSE 500
    LOW 13
    PAUSE 50
    ELSEIF (IN5 = 0) THEN
    LOW 13
    ENDIF
    PAUSE 50
    LOOP



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2003-09-04 03:17
    Here's a cleaner version of the same code. (My Stamp is at work, so I
    couldn't verify the code.) The colon allows you to stack statements on the
    same line, which works rather well.

    You have to uses the DIRS statement to indicate that some of the pins are
    outputs, otherwise they are all inputs and you can get some very flaky
    results from the floating pins. I suspect the lack of a DIRS statement is
    the source of your problem.

    The inputs are scanned constantly until a button is pushed, whereupon the
    light is on for 500 msecs and is then reset.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DIRS $1110000000000000
    LOW 15 : LOW 14 : LOW 13

    DO
    IF (IN3 = 1) THEN HIGH 14 : PAUSE 500 : LOW 14
    IF (IN4 = 1) THEN HIGH 15 : PAUSE 500 : LOW 15
    IF (IN5 = 1) THEN HIGH 13 : PAUSE 500 : LOW 13
    LOOP

    Original Message
    From: "voltsguy" <brianr@d...>
    To: <basicstamps@yahoogroups.com>
    Sent: Wednesday, September 03, 2003 5:14 PM
    Subject: [noparse][[/noparse]basicstamps] Basic I/O problem


    > I am very new to writing code for the basic stamp. I am trying to do
    > a simple 2 in 2 out using the BS2. I wrote the code listed below and
    > each output (14 & 15) will flash, but if both inputs (buttons)are
    > pressed at the same time the outputs follow each other. I understand
    > that the program runs from top down, but is there a way to allow each
    > input to control it's specified output regardless of where it is in
    > the program and independently? Am I way off base with
    > the "IF" "THEN"?
    >
    >
    > ' {$STAMP BS2}
    > ' {$PBASIC 2.5}
    >
    > DO
    >
    > IF (IN3 = 1) THEN
    > HIGH 14
    > PAUSE 500
    > LOW 14
    > PAUSE 500
    > ELSEIF (IN3 = 0) THEN
    > LOW 14
    > ENDIF
    > IF (IN4 = 1)THEN
    > HIGH 15
    > PAUSE 50
    > LOW 15
    > PAUSE 50
    > ELSEIF (IN4 = 0)THEN
    > LOW 15
    > ENDIF
    > IF (IN5 = 1) THEN
    > HIGH 13
    > PAUSE 500
    > LOW 13
    > PAUSE 50
    > ELSEIF (IN5 = 0) THEN
    > LOW 13
    > ENDIF
    > PAUSE 50
    > LOOP
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-09-04 13:00
    Actually, the HIGH and LOW functions take care of setting the DIRS
    register bit for the specified pin so it is not necessary to deal with
    DIRS if you choose to use HIGH and LOW. If, however, you want to work
    with output bits directly, then setting the DIRS bit for your pin is
    necessary.

    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office


    Original Message
    From: MarvL [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=S7TjHdtgl839r7gfi4xGt8r41S9UZLoTG0EhClEqt2KI0aWKJv9ZxVF9MNQ95ZG_qBJVazj3FN0]MarvL@m...[/url
    Sent: Wednesday, September 03, 2003 9:18 PM
    To: basicstamps@yahoogroups.com
    Subject: Re: [noparse][[/noparse]basicstamps] Basic I/O problem


    Here's a cleaner version of the same code. (My Stamp is at work, so I
    couldn't verify the code.) The colon allows you to stack statements on
    the same line, which works rather well.

    You have to uses the DIRS statement to indicate that some of the pins
    are outputs, otherwise they are all inputs and you can get some very
    flaky results from the floating pins. I suspect the lack of a DIRS
    statement is the source of your problem.

    The inputs are scanned constantly until a button is pushed, whereupon
    the light is on for 500 msecs and is then reset.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DIRS $1110000000000000
    LOW 15 : LOW 14 : LOW 13

    DO
    IF (IN3 = 1) THEN HIGH 14 : PAUSE 500 : LOW 14
    IF (IN4 = 1) THEN HIGH 15 : PAUSE 500 : LOW 15
    IF (IN5 = 1) THEN HIGH 13 : PAUSE 500 : LOW 13
    LOOP

    Original Message
    From: "voltsguy" <brianr@d...>
    To: <basicstamps@yahoogroups.com>
    Sent: Wednesday, September 03, 2003 5:14 PM
    Subject: [noparse][[/noparse]basicstamps] Basic I/O problem


    > I am very new to writing code for the basic stamp. I am trying to do a

    > simple 2 in 2 out using the BS2. I wrote the code listed below and
    > each output (14 & 15) will flash, but if both inputs (buttons)are
    > pressed at the same time the outputs follow each other. I understand
    > that the program runs from top down, but is there a way to allow each
    > input to control it's specified output regardless of where it is in
    > the program and independently? Am I way off base with the "IF" "THEN"?
    >
    >
    > ' {$STAMP BS2}
    > ' {$PBASIC 2.5}
    >
    > DO
    >
    > IF (IN3 = 1) THEN
    > HIGH 14
    > PAUSE 500
    > LOW 14
    > PAUSE 500
    > ELSEIF (IN3 = 0) THEN
    > LOW 14
    > ENDIF
    > IF (IN4 = 1)THEN
    > HIGH 15
    > PAUSE 50
    > LOW 15
    > PAUSE 50
    > ELSEIF (IN4 = 0)THEN
    > LOW 15
    > ENDIF
    > IF (IN5 = 1) THEN
    > HIGH 13
    > PAUSE 500
    > LOW 13
    > PAUSE 50
    > ELSEIF (IN5 = 0) THEN
    > LOW 13
    > ENDIF
    > PAUSE 50
    > LOOP
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject
    > and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >



    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
Sign In or Register to comment.