Help with SELECT/CASE and INA - CLOSED
davejames
Posts: 4,047
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
(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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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)
·
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
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)
·
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
(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