Shop OBEX P1 Docs P2 Docs Learn Events
Source for more information on "Select... Case" statements — Parallax Forums

Source for more information on "Select... Case" statements

ZeusZeus Posts: 79
edited 2011-09-02 09:30 in BASIC Stamp
To All,

I need more detailed information on "Select... Case" statements and how to employ them. Specifically when using them with pull down push buttons.

I developed a subroutine with pull up push buttons and it was running perfectly but when I went to integrate it into the main program, with pull downs, everything stops working. I have managed to get the bulk of the minor problems solved however in doing this I came to realize that the overall/bigger problem is my lack of understanding of how a "Select... Case" functions at higher levels.

I have looked through "What's a Microcontroller?" & the "Basic Stamp Syntax & Reference Manual" but these do not seem to address my situation. It seems odd to me that a pull down would have this effect on a statement.

Also I have adjusted my inputs to look for a pull down voltage.

Thanks in advance.... Zeus

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-08-31 18:05
    Zeus wrote:
    I have looked through "What's a Microcontroller?" & the "Basic Stamp Syntax & Reference Manual" but these do not seem to address my situation. It seems odd to me that a pull down would have this effect on a statement.
    Select... Case is like several IF statements. How about posting your problematic code and a schematic.
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-01 06:50
    Zeus,

    As Mike has noted, posting code and schematic will help with a solution.

    In general terms, if the code worked before changing the logic of the push buttons, it can be made to work after changing the switches. The only difference is that when the program comes to the "Select...." part, your changed switches present a different number than before. Take that into account and things will be as you had them before.

    Cheers,
  • ZeusZeus Posts: 79
    edited 2011-09-01 17:26
    Mike & Stamptrol,

    Here is a bit of the code as requested. I did not post all of it as the structure repeats but the problem is the same.
    'Alarm Hour Buttons Block
    IF (MenuNum = 0) THEN
    SELECT IND & 3 'Start of "Select Case" statement, possibilities are 00, 01, 10 & 11.

    CASE = 3 'Case 3, both buttons pressed... Reset
    AlarmHrs = 6 'If both buttons are pressed alarm hours to 6.
    CASE = 2 'Case 2, one button is pressed... advance 1.
    AlarmHrs = AlarmHrs + 1
    IF AlarmHrs = 13 THEN AlarmHrs = 1 'Starts variable counting over at 1 if value > 12.

    CASE 1 'Case 1, other button is pressed... subtract 1.
    AlarmHrs = AlarmHrs - 1
    IF AlarmHrs = 0 THEN AlarmHrs = 12 'Starts variable counting over at 12 if value < 1.
    ENDSELECT
    ENDIF
    IN12, IN13, IN14 & IN15 Serve to scroll through the clock menu and adjust the hours and minutes, see below... (I left out the program comments as they are wordy.)
    DO
    LOOP UNTIL (IN12 = 0) OR (IN13 = 0) OR (IN14 = 0) OR (IN15 = 0)
    GOSUB Buttons_Sub
    PAUSE 50
    LCDOut = LCDOut + 1
    LOOP UNTIL (IN12 = 0) OR (IN13 = 0) OR (IN14 = 0) OR (IN15 = 0) OR (LCDOut = 80) SEROUT 6, 84, [18]
    I am sure that this is just a matter of configuring my statement to account for the fact that I will now be looking for a "0" rather than "1". What am I missing?

    Thanks... Zeus
  • stamptrolstamptrol Posts: 1,731
    edited 2011-09-02 07:39
    Zeus,

    You've got it exactly right! It may also be clearer if you get rid of the & in the SELECT statement.

    Assume we agree that your statement "...I went to integrate it into the main program, with pull downs," means that the variable IND&3 is equal to 3 when no switches are pushed.

    By getting rid of the & (the AND) operator, the cases are then 1 when in12 is pushed, 2 when in13 is pushed, 4 when in14 is pushed and 8 when in15 is pushed. It follows that in12 and in13 being pushed at the same time will give 3.

    Cheers,
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-09-02 09:30
    Hi Zeus,

    Another way to deal with active low buttons is to convert the variable using the Stamps ~ operator (binary complement) so that lows become highs:
    x VAR Nib
    x = ~inD    '  e.g.,   %1111 becomes %0000,   %1101 becomes %0010
    
    As Stamptrol said, get rid of the &3. There are 15 possible active cases. You can use CASE ELSE to deal with the ones that are to be excluded.

    Internally Stamp compiler translates the SELECT:CASE statements to a series of IF:GOTO logic with labels that are hidden from view. It may help you understand the logic if you see how the compiler makes that translation. I have a little writeup about it here.

    Another little observations, since you are interested in coding, this statement:
    IF (IN12 = 0) OR (IN13 = 0) OR (IN14 = 0) OR (IN15 = 0) ...
    
    can probably be simplified with
    IF IND <> %1111
    
    It can help us follow your code if you enclose it in code tags and use indentation.
    attachment.php?attachmentid=78421&d=1297987572
Sign In or Register to comment.