Shop OBEX P1 Docs P2 Docs Learn Events
Help with SELECT/CASE and INA - CLOSED — Parallax Forums

Help with SELECT/CASE and INA - CLOSED

davejamesdavejames Posts: 4,047
edited 2009-11-04 01:06 in BASIC Stamp
Hi All,

(Using a BS2 w/PBasic 2.5)

I have the need to test P1, P2 and P3 for a 'high', and I thought the SELECT/CASE approach would be suitable.· So the initially, the code was:

DIRS $1111111111110000 'pins 0-3 inputs, 4-15 outputs

testpin PIN 0

DO WHILE (testpin) 'while P0 true
···· SELECT INA
········· CASE INA.BIT1 'P1 true
·············· GOSUB DoSomething1
········· CASE INA.BIT2 'P2 true
·············· GOSUB DoSomething2
········· CASE INA.BIT3 'P3 true
·············· GOSUB DoSomething3
···· ENDSELECT
LOOP
END

DoSomething1:
···· DEBUG "...do something 1", CR
RETURN

DoSomething2:
···· DEBUG "...do something 2", CR
RETURN

DoSomething3:
···· DEBUG "...do something 3", CR
RETURN
·
When this code is run, I never see the "...do something" messages when the appropriate BS2 pin is 'high', so I'm guessing that·I am using the SELECT/CASE construct incorrectly or I'm not understanding the intricacies of INS and INA.
·
If the code is modified as below, I do see the messages:
·
DO WHILE (testpin) 'while P0 true
···· IF IN1 THEN 'P1 true
········· GOSUB DoSomething1
···· ELSEIF IN2 THEN·'P2 true
········· GOSUB DoSomething2
···· ELSEIF IN3 THEN·'P3 true
········· GOSUB DoSomething3
···· ENDIF
LOOP

Since the above works, I can go ahead and use it...but I'd still like clarification on why the SELECT/CASE approach doesn't.
·
Any thoughts would be appreciated,
·
DJ

·

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Instead of:

"Those who can, do.· Those who can't, teach." (Shaw)
I prefer:
"Those who know, do.· Those who understand, teach." (Aristotle)


Post Edited (davejames) : 11/4/2009 5:13:49 PM GMT

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-11-03 19:23
    I think you need to assign INA to a variable to test it. I may be wrong but the manual should give you the correct usage.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • davejamesdavejames Posts: 4,047
    edited 2009-11-03 21:19
    Thanks Franklin,

    One of the things I tried was assigning INA to a variable; no luck - maybe I goofed it. And the manual is not clear concerning the use of SELECT/CASE with INA.

    Later,

    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Instead of:

    "Those who can, do.· Those who can't, teach." (Shaw)
    I prefer:
    "Those who know, do.· Those who understand, teach." (Aristotle)
    ·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-03 21:34
    inA can have 16 possible states, but your SELECT:CASE code won't catch any of them. When p1, p2 or p3 is high in addition to p0, the value of inA will be a value greaater than one, but each of the CASEs is testing that value (>1) against a bit that is either zero or one.
    DO WHILE (testpin) 'while P0 true
         SELECT INA          '<---- this is greater than 1 when both p0 and any other pin is high
              CASE INA.BIT1 'P1 true    ' <------  each CASE is either 0 or 1
                   GOSUB DoSomething1
              CASE INA.BIT2 'P2 true
                   GOSUB DoSomething2
              CASE INA.BIT3 'P3 true
                   GOSUB DoSomething3
         ENDSELECT
    LOOP
    



    You would do better with the straight IF:THEN:ELSEIF construct. That is going to give p1 priority and will continue to execute the p1 action repeatedly until p1 goes back low. Is that what you want?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • davejamesdavejames Posts: 4,047
    edited 2009-11-03 21:58
    Mr. Allen - thank you for the insight.

    Yes, I understand that INA can have 16 possible values (0 - 15) represented by the state of the bits in the group of 4 (0 - 3).

    My thinking was that INA.BIT1 represented PIN1, BIT2 for PIN2, and BIT3 for PIN3.

    The SELECT part 'selects' an item, and then the subsequent CASEs test for a condition. I thought I could 'select' INA, and then test on a particular BIT 'case' of being 1 or 'true'.

    Honestly, I can't see where P0 being true would affect anything other than the DO loop so I'm unclear of what you were attempting to tell me with the second sentence.

    BTW - inside of each "DoSomething", a routine resets the active BIT to 'low'.

    As you noted, the IF:THEN:ELSEIF construct works. Since you seem to be the "Father of Basic Stamp Execution Times", does the I:T:E construct operate faster/slower than a SELECT/CASE? I couldn't find that info on your Stamp webpage.

    Regards,

    DJ

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Instead of:

    "Those who can, do.· Those who can't, teach." (Shaw)
    I prefer:
    "Those who know, do.· Those who understand, teach." (Aristotle)
    ·
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2009-11-03 22:48
    You are correct in thinking "that INA.BIT1 represented PIN1, BIT2 for PIN2, and BIT3 for PIN3". However, you are misinterpreting how the SELECT works. SELECT inA is not selecting the inA port as a source of data for the following cases, instead, inA is just a number, the current value on the port inA. That number will be an odd number, because p0 is normally high in order for the DO:LOOP to execute. The code goes on to read the state of individual pins p1..p3. If those pins are all low, then the SELECT value is 1, and the individual bits are all zero, so no branch is taken. But say both p0 and p2 are high, then inA=5 and inA.BIT2=1, and there is no way that 5=1. Does that clear it up more? p0 affects the result because it is part of the number that comes from reading inA.

    The SELECT:CASE structure is quite efficient when used in its intended manner. Note that SELECT:CASE is a construct that was introduced in PBASIC 2.5, and it is built up by the IDE from the primitive IF:THEN tokens that make up the BASIC Stamp's internal instruction set. See www.emesystems.com/BS2pbasic25.htm for an idea of how that works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • davejamesdavejames Posts: 4,047
    edited 2009-11-04 01:06
    ...oooooOOOOOOHHHHHHHHHHHhhhhhh!!!!! I get it now...yes...INA is just a number and I'm expecting SELECT/CASE to treat it as an array or structure from which I can pick out bits & pieces. Right, and P0 gets its fingers in the pie too. Got it.

    (Duh Dave! Jeese louise!)

    I'll check out the mentioned portion of your sight for more info.


    Thank you for your indulgence and help,

    DJ

    110409 Update - switched the code over to use the IF:THEN:ELSEIF approach.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Instead of:

    "Those who can, do.· Those who can't, teach." (Shaw)
    I prefer:
    "Those who know, do.· Those who understand, teach." (Aristotle)


    Post Edited (davejames) : 11/4/2009 5:14:58 PM GMT
Sign In or Register to comment.