Shop OBEX P1 Docs P2 Docs Learn Events
Syntax help with IN — Parallax Forums

Syntax help with IN

ArchiverArchiver Posts: 46,084
edited 2003-07-07 17:15 in General Discussion
Suppose I have a BS2 pin 10 connected to an input switch.
e.g. SW1 CON 10

Later, I want to read pin 10's state. I've been using statement like
IF IN10=1 THEN Label1

But this is not "elegant" if I have many input switches, and not very
legible.

Is there a way to check for input by using name of the pin (SW1) in
this case?

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-07-07 14:38
    I know the new software has a clean way to handle this (PIN, I think,
    but I'm sure Jon will chime in). With the current software, try this:

    SW1 CON 10
    SW1p VAR IN10


    IF SW1p = 1 then Label1

    Al Williams
    AWC
    * New Kits:
    http://www.al-williams.com/kits.htm




    >
    Original Message
    > From: basicstampede [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=Y8EpDkjsQKNPgN-4hg68djVkv_yFNeYM0b5TNuHsmXkQMNIIS4vytfSvaFVzdaLW6KJ1-X262l-OSYjjgA]basicstampede@y...[/url
    > Sent: Monday, July 07, 2003 6:59 AM
    > To: basicstamps@yahoogroups.com
    > Subject: [noparse][[/noparse]basicstamps] Syntax help with IN
    >
    >
    > Suppose I have a BS2 pin 10 connected to an input switch. e.g. SW1 CON

    > 10
    >
    > Later, I want to read pin 10's state. I've been using statement like
    > IF IN10=1 THEN Label1
    >
    > But this is not "elegant" if I have many input switches, and not very
    > legible.
    >
    > Is there a way to check for input by using name of the pin (SW1) in
    > this case?
    >
    >
    >
    >
    > 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-07-07 15:02
    There are a couple of ways to do this, but the most "elegant" (IMHO) is
    with the PIN defintion and a couple of constants. Like this:


    StartSw PIN 10

    IsOn CON 1
    IsOff CON 0

    ...

    IF (StartSw = IsOn) THEN Label1 ' is the same as IF
    (IN10 = 1) THEN Label1


    I'm a very big fan of "elegant" programs. So much so that I put
    together a document we use interally called "The Elements of PBASIC
    Style." If you have the newest compiler, you'll find the document in
    the Help file, otherwise you can download the PDF from this link:

    http://www.parallax.com/html_pages/downloads/basicstamps/documentation_b
    asic_stamp.asp


    Just a note about the PIN defintion. The compiler is smart enough to
    know what you're doing with a pin and will accordingly. If, for
    example, we also had an LED we could do this:


    RunLED PIN 8


    Since outputs need to have their DIRS bit set too, the easiest way to
    initialize the LED off is by doing this:


    Init:
    LOW RunLED ' same as LOW 8


    If you want to initialize the LED as on, use HIGH instead. Now the LED
    can be controlled with the previously-defined constants:


    RunLED = IsOn ' same as OUT8 = 1

    or...

    RunLED = IsOff ' same as OUT8 =
    0


    -- Jon Williams
    -- Parallax



    Original Message
    From: basicstampede [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=kyVARWEPVrGX-inhh8eGvdPF3uhwlWtEW4qD61PZahIJkZLqQvla9iGUD26ZAkBam6BhZwD2NiIexOpPSBYR8A]basicstampede@y...[/url
    Sent: Monday, July 07, 2003 6:59 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Syntax help with IN


    Suppose I have a BS2 pin 10 connected to an input switch.
    e.g. SW1 CON 10

    Later, I want to read pin 10's state. I've been using statement like
    IF IN10=1 THEN Label1

    But this is not "elegant" if I have many input switches, and not very
    legible.

    Is there a way to check for input by using name of the pin (SW1) in
    this case?




    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-07-07 17:15
    Yes, this use of the VAR syntax needs a little
    explanation.

    When you say "SW1_in VAR IN10", you are not
    'really' declaring a variable, you are declaring
    an 'alias' for IN10. It doesn't take 'ram'
    space, it's a signal for the compiler.

    You can now read the state of the pin
    (assuming you did INPUT SW1 at some point)
    with: MyVal = SW1_in, or IF SW1_in = 1 THEN...

    You can also have an "SW1_out VAR OUT10" so
    that you can write the state of the pin with
    "SW1_out = 1", or "SW1_out = 0". Again, this
    assumes you've done an OUTPUT SW1 at some
    point.

    The 'new' compiler (2.5) lets you declare
    SW1 PIN 10. Now, the compiler will let you
    do MyVal = SW1, or SW1 = MyVal, and both
    will work.


    --- In basicstamps@yahoogroups.com, "Al Williams" <alw@a...> wrote:
    > I know the new software has a clean way to handle this (PIN, I
    think,
    > but I'm sure Jon will chime in). With the current software, try
    this:
    >
    > SW1 CON 10
    > SW1p VAR IN10
    >
    >
    > IF SW1p = 1 then Label1
    >
    > Al Williams
    > AWC
    > * New Kits:
    > http://www.al-williams.com/kits.htm
    >
    >
    >
    >
    > >
    Original Message
    > > From: basicstampede [noparse][[/noparse]mailto:basicstampede@y...]
    > > Sent: Monday, July 07, 2003 6:59 AM
    > > To: basicstamps@yahoogroups.com
    > > Subject: [noparse][[/noparse]basicstamps] Syntax help with IN
    > >
    > >
    > > Suppose I have a BS2 pin 10 connected to an input switch. e.g.
    SW1 CON
    >
    > > 10
    > >
    > > Later, I want to read pin 10's state. I've been using statement
    like
    > > IF IN10=1 THEN Label1
    > >
    > > But this is not "elegant" if I have many input switches, and not
    very
    > > legible.
    > >
    > > Is there a way to check for input by using name of the pin (SW1)
    in
    > > this case?
    > >
    > >
    > >
    > >
    > > 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/
    > >
    > >
    > >
Sign In or Register to comment.