IF...ELSEIF Questions
kf4mat
Posts: 21
Hi all,
I am currently working on the WAM text chapter 3 and have a quick question about the if...elseif statement. I looked in the help file and it does not say wether or not what I am doing is considered correct so here goes. How many ELSEIF statements can you have? If you modify the code as directed on page 86 and push both buttons both LED's flash, however unless I am reading incorrectly it only contains code to flash the led on P14 if only one button is pushed (IN3).
My solution was to add another elseif statement, which worked, but is this how it should be done or is there a more elegant way to do this????
Tom
Here is a copy of my activity #4 code
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
· DEBUG HOME
· DEBUG ? IN3
· DEBUG ? IN4
· IF (IN3 = 1) AND (IN4 = 1) THEN
· HIGH 14
· HIGH 15
· PAUSE 50
· ELSEIF (IN3 = 1) THEN
· HIGH 14
· PAUSE 50
· ELSEIF (IN4 = 1) THEN
· HIGH 15
· PAUSE 50
· ELSE
··· PAUSE 100
· ENDIF
· LOW 14
· LOW 15
· PAUSE 50
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
[noparse][[/noparse]img][noparse][[/noparse]URL=http://img362.imageshack.us/my.php?image=license200508200801546022311gg.jpg][noparse][[/noparse]IMG]http://img362.imageshack.us/img362/1403/license200508200801546022311gg.th.jpg[noparse][[/noparse]/IMG][noparse][[/noparse]/URL] [noparse][[/noparse]/img]
I am currently working on the WAM text chapter 3 and have a quick question about the if...elseif statement. I looked in the help file and it does not say wether or not what I am doing is considered correct so here goes. How many ELSEIF statements can you have? If you modify the code as directed on page 86 and push both buttons both LED's flash, however unless I am reading incorrectly it only contains code to flash the led on P14 if only one button is pushed (IN3).
My solution was to add another elseif statement, which worked, but is this how it should be done or is there a more elegant way to do this????
Tom
Here is a copy of my activity #4 code
' {$STAMP BS2}
' {$PBASIC 2.5}
DO
· DEBUG HOME
· DEBUG ? IN3
· DEBUG ? IN4
· IF (IN3 = 1) AND (IN4 = 1) THEN
· HIGH 14
· HIGH 15
· PAUSE 50
· ELSEIF (IN3 = 1) THEN
· HIGH 14
· PAUSE 50
· ELSEIF (IN4 = 1) THEN
· HIGH 15
· PAUSE 50
· ELSE
··· PAUSE 100
· ENDIF
· LOW 14
· LOW 15
· PAUSE 50
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
[noparse][[/noparse]img][noparse][[/noparse]URL=http://img362.imageshack.us/my.php?image=license200508200801546022311gg.jpg][noparse][[/noparse]IMG]http://img362.imageshack.us/img362/1403/license200508200801546022311gg.th.jpg[noparse][[/noparse]/IMG][noparse][[/noparse]/URL] [noparse][[/noparse]/img]
Comments
· testIn = (INL & %00011000) >> 3
· SELECT testIn
··· CASE %11
····· HIGH 14
····· HIGH 15
····· PAUSE 50
··· CASE %01
····· HIGH 14
····· PAUSE 50
··· CASE %10
····· HIGH 15
····· PAUSE 50
··· CASE ELSE
····· PAUSE 100
· ENDSELECT
· LOW 14
· LOW 15
· PAUSE 50
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 10/16/2005 3:59:19 AM GMT
Have I missed something? Why is the extra variable is "required" in the SELECT statement?
SELECT ((INL & %00011000) >> 3)
is perfectly acceptable to the PBASIC 2.5 compiler. True, it's nice if you can spare the RAM
but when things get tight.... definitely not required.
</mode>
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Rusty-
--
Rusty Haddock = KD4WLZ = rusty@fe2o3.lonestar.org
**Out yonder in the Van Alstyne (TX) Metropolitan Area**
Microsoft is to software what McDonalds is to gourmet cooking
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Thanks for the input,
Tom
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
· temp = INL
At this point temp will be equal to %00011000.· Now if we do this:
· temp = temp >> 3
... temp will end up with %00000011.
See how the bits were shifted to the right?· There is a complimentary operator, <<, that shifts bits left.· On another note, shifting right by one bit is the same as dividing by two; shifting-left by one bit is the same as multiplying by two.
So...
· temp = temp >> 3
... is actually the same as:
· temp = temp / 8
... but shifting is more efficient (internally) than division.· Advanced programmers with look for power-of-2 division and multiplication opportunies to use shift operators.· You could replace:
· myValue = testInput * 32
... with:
· myValue = testInput << 5
... and the code will run a little faster.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 10/16/2005 7:18:05 PM GMT
Just add the following information to what Jon has already supplied, and you'll be able to see exactly what he did with his tricky bit of coding:
(INL & %00011000) >> 3
The pin ports on the PBASIC Stamp can be addressed individually (In0, In1, In2, etc for input), in four groups of four successive pins (INA, INB, INC, IND for input) or in two groups of eight successive pins (INL, INH or Input Low, Input High). Thus INL addresses Input pin ports 0-7 collectively, as one unit. What was said above about the INPUT pins is also true of the OUTPUT pin ports as well (Out1, Out2, ...; OUTA, OUTB, ...; OUTL, OUTH, etc).
If you want to set a bit ON, you use the bitwise OR operator (|) with a 1 in the mask position where you want the bit turned ON. If you want to set a bit OFF, you use the bitwise AND operator (&) and use a 0 in the mask position where you want the bit turned OFF. This can be used for single bits (a one bit mask) or multiple bits (a multi-bit mask). In this piece of coding, a multi-bit mask is being used ("%00011000"), 8 bits wide.
Thus, in the section of that instruction "INL & %00011000", the first operation (INL) is to field the status of Input pin ports 0-7 and place the result in an unseen, temporary variable. The second succesive operation "&" (bitwise AND) uses that same temporary variable as the source field, and sets everything EXCEPT the bits that represent the state of Input pin ports 3 and 4 to zero or OFF, since they are presently of no interest to us. The bits representing the state of Input pin ports 3 and 4·are left alone. In the portion of this instruction string we're looking at now, INL is the original source field, an unseen temporary variable is the receiving field for the first operation, and "%00011000" is the "bit mask" of which I'm speaking. The mask bits are applied to the temporary variable; thus it is the source field for the second operation.
As Jon said in his reply, in the last segment,or third operation,·of this instruction ">> 3" the result of all that, which is still contained within the temporary variable, is effectively "moved" to the right (shifted right) three bit positions, leaving the result in the 2 LSB positions of the unseen temporary variable, and zero filling the "emptied" MSB bits.
I hope that's helpful and not more confusing.
Regards,
Bruce Bates
Post Edited (Bruce Bates) : 10/16/2005 8:47:40 PM GMT
Your solution is correct.· It was the conclusion I was hoping you would draw based on what you have learned so far in What's a Microcontroller.
One improvement you can make to the·program you posted is simply to indent the code blocks·after each IF, ELSEIF, ELSE condition.· Here is an example:
Notice that everything between DO and LOOP is indented by at least two spaces.· It helps the reader see that everything is·happening in a single DO...LOOP.· In addition, the code blocks that follow each (IF...ELSEIF...ELSE...) are indented two more spaces.· This makes it easier to see what action the program takes for each condition.
If you have more questions as you go through the activities in What's a Microcontroller, consider posting them to the Stamps in Class forum.· In addition to other members who have worked through or are working through the same books, most if not all of the·Stamps in Class authors monitor that forum for questions that pertain to their books.·
Regards, Andy
Post Edited (Andy Lindsay (Parallax)) : 10/17/2005 3:57:57 AM GMT
Tom
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔