Shop OBEX P1 Docs P2 Docs Learn Events
select case — Parallax Forums

select case

cplattcplatt Posts: 55
edited 2009-04-02 06:31 in BASIC Stamp
Could someone give me a little guidance please?

Using the Select Case command the max number of Cases is apparently 16 or 17. I have over 25 conditions I am testing. (Basically if x=1 go to this sub...through if x=25 go there.) Is there a better way than using one Select command to test 1-17 and 8 more if then commands for numbers 18- 25?

thanks so much

curtis.

Comments

  • JDJD Posts: 570
    edited 2009-04-01 06:27
    Curtis,

    Can you post the code you currently have? It is easier to understand the program if we can look at it. It sounds like you might be able to use a LOOKDOWN/LOOKUP command,·because you can have 256 options in each of those commands.

    Do·you have the BASIC Stamp Syntax & Refence Manual? If not, here is a link to the PDF free download.

    BASIC Stamp Syntax & Refence Manual:
    http://www.parallax.com/Portals/0/Downloads/docs/prod/stamps/web-BSM-v2.2.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Respectfully,


    Joshua Donelson
    www.parallax.com
  • cplattcplatt Posts: 55
    edited 2009-04-01 14:28
    Joshua, not to oversimplify but this is pretty much it:

    select x

    case 1
    goto a
    case 2
    goto b
    case 3
    goto c
    case 4
    goto d
    case5
    goto e
    etc.
    case 25
    go to y
    end select
    I understand the case command is very efficient since the program stops once it finds a match and returns to the call that way it doesn't have to continue to check. thank you so much!
  • cplattcplatt Posts: 55
    edited 2009-04-01 14:36
    Might this work to "fool" the stamp?

    If x<10 then

    select x
    case 1
    goto a
    etc.
    case 9
    goto i
    end select
    endif

    if x>=10
    y=x
    select y
    case1
    goto j
    etc etc
  • Lee HarkerLee Harker Posts: 104
    edited 2009-04-01 14:58
    Curtis,

    It looks like you will want to use the Branch command. That would allow you to goto up to 255 different locations in your program. I think it would help if you post your code so you could get more help. It has been my experience that if you think there is a need for an excessive number of branches that there·could be fundemental changes·in the code that could make things much better.
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-01 15:12
    You are right - I get an error saying that limit of 16 case statement was exceeded.
    So the clean option is - use ·LOOKUP.
    Of course your workaroud ( fancy word for kluge ) would work just fine.

    My solution - ON Index GOSUB - of course you need to make sure your Index is correct and build some dummies in your code.

    Here is atest code.


    Index VAR Byte
    Main:
    FOR· Index = 0 TO 25
    · ON Index GOSUB Goto_sub,Goto_sub,Goto_sub,Goto_sub, Goto_sub,Goto_sub,Goto_sub,Goto_sub,· Goto_sub,Goto_sub,Goto_sub,Goto_sub, Goto_sub,Goto_sub,Goto_sub,Goto_sub, Goto_sub,Goto_sub,Goto_sub,Goto_sub, Goto_sub,Goto_sub,Goto_sub,Goto_sub,· Goto_sub,Goto_sub,Goto_sub,Goto_sub, Goto_sub,Goto_sub,Goto_sub,Goto_sub
    NEXT
    STOP

    END
    GOTO_SUB:
    · DEBUG ? Index
    RETURN

    ·
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-04-01 15:14
    Whoops _ I forgot to delete the "LOOKUP" solution sentence.
  • JDJD Posts: 570
    edited 2009-04-01 16:04
    Curtis,

    You would be able to use some IF...THEN statements to break up the SELECT...CASE; because·a program can not have more than 16 nested CASE. However you can break it up in parts and should work just fine if you wanted to go that route.

    I attached some code that compiled just·fine if you wanted to go that·route. Mind the memory map though, just for sheer size of the program. Again there might be a way to do this·more efficient with a LOOKUP or·LOOKDOWN command; but maybe this will get you started.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Respectfully,


    Joshua Donelson
    www.parallax.com
  • cplattcplatt Posts: 55
    edited 2009-04-01 16:08
    thank you for all the help! the branch otta work just fine, but here is my code, the Select is half way thru:

    [noparse][[/noparse]code]

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    ' this app receives asyncronous serial data via com port to run led indicators and 7 segment digital led displays for a flight simulator.

    DATA_n1 CON 6 'first 7219 display module
    LoadCS1 CON 5
    CLK1 CON 4

    DATA_n2 CON 3 'second 7219 display module
    LoadCS2 CON 2
    CLK2 CON 1


    decode CON 9
    brite CON 10
    scan CON 11
    switch CON 12
    test CON 15

    '~~~~~~~~~~~~~~~~~~~variables~~~~~~~~~~~~~~~~~~

    serDataIn VAR Word '0-65535 or something
    PINN VAR Byte '0-255
    max_dat VAR Word
    index VAR Nib
    temp VAR Nib
    nonZ VAR Bit
    dispVal VAR Word
    odd VAR index.BIT0
    x VAR Byte
    GO VAR Byte

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~initialize 7219~~~~~~~~~~~~~~~~~~
    FOR x=0 TO 1 'repeat twice times for good measure

    MAINIO
    FOR index=0 TO 7
    LOOKUP index,[noparse][[/noparse]scan,7,brite,1,decode,$FF,switch,1],max_dat 'toggle test1 or test0 o test but take out to run!!!!
    SHIFTOUT Data_n1, CLK1,MSBFIRST,[noparse][[/noparse]max_dat]
    IF odd=0 THEN noLoad1
    PULSOUT LoadCS1,5
    NoLoad1:
    NEXT

    FOR index=0 TO 7
    LOOKUP index,[noparse][[/noparse]scan,7,brite,1,decode,$FF,switch,1],max_dat 'toggle test1 or test0 o test but take out to run!!!!
    SHIFTOUT Data_n2, CLK2,MSBFIRST,[noparse][[/noparse]max_dat]
    IF odd=0 THEN noLoad2
    PULSOUT LoadCS2,5
    NoLoad2:
    NEXT
    PAUSE 1000

    NEXT


    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~zero~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    MAINIO
    dispVal= 0 'blankit or zeroit here
    FOR index=1 TO 8
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]index]
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]dispVal]
    PULSOUT LoadCS2,5
    PAUSE 100
    NEXT

    dispVal= 0
    FOR index=1 TO 8
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]index]
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]dispVal]
    PULSOUT LoadCS1,5
    PAUSE 100
    NEXT

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    comcheck:
    MAINIO
    'SEROUT 16, 17405,[noparse][[/noparse]"r"] ' stamp not sending(listening)
    SERIN 16, 17405, 1000, SPST, [noparse][[/noparse]DEC serDataIn] 'baud rate very import bs2p has own!!!!

    GO=serDataIN DIG(0)+(serDataIn DIG(1)*10)


    SPST:
    AUXIO


    SELECT GO
    CASE 11 'AP on/off
    LOW 5
    CASE 12 'AP on/off
    HIGH 5
    CASE 15 'HDGH off
    LOW 10
    CASE 16 'HDGH on
    HIGH 10
    CASE 17 'SPDH off
    LOW 14
    CASE 18 'SPDH on
    HIGH 14
    CASE 21 'APP off
    LOW 7
    CASE 22 'APP on
    HIGH 7
    CASE 23 'LOC off
    LOW 6
    CASE 24 'LOC on
    HIGH 6
    CASE 25 'VS off
    LOW 9
    CASE 26 'VS on
    HIGH 9
    CASE 27 'ALTH off
    LOW 8
    CASE 28 'ALTH on
    HIGH 8

    CASE 29 'gearup
    LOW 3
    HIGH 4
    PAUSE 1000
    HIGH 3
    HIGH 4

    ' !!!!!!Select Stop here due to MAX # of Case=16

    ' IF GO=30 THEN 'geardown
    ' LOW 3
    ' HIGH 4
    ' PAUSE 1000
    ' LOW 3
    ' LOW 4
    ' ENDIF

    ' IF GO =31 THEN GOTO HDGDisplay '31 hdg 31 to 36031
    ' IF GO=32 THEN GOTO ALTDisplay '32 alt 32 to 65532 one word (byte) max
    ' IF GO=33 THEN GOTO ASPDisplay '33 ASP 33 to 65533 one word (byte) max
    ' IF GO=34 THEN GOTO VSDisplay '34 +VS 34 to 9934
    ' IF GO=35 THEN GOTO VSnegDisplay '35 -VS 35 to 9935

    ENDSELECT

    'GOTO HDGDisplay testing block only
    'GOTO ALTDisplay
    'GOTO ASPDisplay
    'GOTO VSDisplay
    'GOTO VSnegDisplay


    PAUSE 50
    GOTO comcheck

    HDGDisplay:
    MAINIO
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]6] 'code 36020 2-3-4 0=DIG0,6=DIG1,3=DIG3,7=DIG4,1=DIG5
    temp=serDataIn DIG (2)
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS2,5
    PAUSE 50

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,
    temp=serDataIn DIG (3)
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS2,5
    PAUSE 50

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,
    temp=serDataIn DIG (4)
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS2,5
    PAUSE 50

    GOTO comcheck

    ALTDisplay:
    MAINIO
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]6] '99921 6-5-4 index
    temp=serDataIn DIG (2) ' 2-3-4 0=DIG0,6=DIG1,3=DIG3,7=DIG4,1=DIG5
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS1,5
    PAUSE 50

    SHIFTOUT DATA_n1,CLK1,MSBFIRST,
    temp=serDataIn DIG (3)
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS1,5
    PAUSE 50

    SHIFTOUT DATA_n1,CLK1,MSBFIRST,
    temp=serDataIn DIG (4)
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS1,5
    PAUSE 50

    GOTO comcheck

    ASPDisplay:
    MAINIO
    SHIFTOUT DATA_n2,CLK2,MSBFIRST, '99922 3-2-1 index
    temp=serDataIn DIG (2) ' 2-3-4 0=DIG0,6=DIG1,3=DIG3,7=DIG4,1=DIG5
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS2,5
    PAUSE 50

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,
    temp=serDataIn DIG (3)
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS2,5
    PAUSE 50

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,
    temp=serDataIn DIG (4)
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp]
    PULSOUT LoadCS2,5
    PAUSE 50
    GOTO comcheck

    VSDisplay:

    MAINIO
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]7] 'tells what digit
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]0] 'tells value to clock in
    PULSOUT LoadCS2,5 'this zeros (the negative sign)
    PAUSE 50

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]8] 'tells what digit
    temp=serDataIn DIG (2) '7-8-1 17362=0=DIG0,6=DIG1,3=DIG3,7=DIG4,1=DIG5
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp] 'tells value to clock in
    PULSOUT LoadCS2,5
    PAUSE 50

    SHIFTOUT DATA_n1,CLK1,MSBFIRST, 'tells what digit
    temp=serDataIn DIG (3) '17360 0=DIG0,6=DIG1,3=DIG3,7=DIG4,1=DIG5
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]temp] 'tells value to clock in
    PULSOUT LoadCS1,5
    PAUSE 50

    GOTO comcheck

    VSnegDisplay:

    MAINIO

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]7] 'tells what digit
    temp=serDataIn DIG (2)
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]10] 'tells value to clock in
    PULSOUT LoadCS2,5 'this adds the negative sign
    PAUSE 50

    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]8]
    temp=serDataIn DIG (3) '9923 tells what digit
    SHIFTOUT DATA_n2,CLK2,MSBFIRST,[noparse][[/noparse]temp] 'tells value to clock in
    PULSOUT LoadCS2,5
    PAUSE 50

    SHIFTOUT DATA_n1,CLK1,MSBFIRST, 'tells what digit
    SHIFTOUT DATA_n1,CLK1,MSBFIRST,[noparse][[/noparse]temp] 'tells value to clock in
    PULSOUT LoadCS1,5
    PAUSE 50


    GOTO comcheck
    'DIG 12 DIG(0)=2 DIG(1)=1
    'DIG -1234 DIG(5)=0 DIG(0)=4 DIG(1)=3 DIG(2)=2 DIG(3)=1
    ' SPD HDG V/S ALT
    'digit 1-16 left to right 1-2-3 4-5-6 7-8-1-2-3 4-5-6-7-8 for CS2 chip/CS1 chp second group
    'value 10=- 0=0 1=1 2=2 3=3 ...

    RETURN
  • JDJD Posts: 570
    edited 2009-04-01 16:21
    Curtis,

    Just as a future note, if you post complete code, normally it is easier for the reader if you use the attachement manager; unless it is just a snippet of code. Not a big deal a heads up

    smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Respectfully,


    Joshua Donelson
    www.parallax.com
  • cplattcplatt Posts: 55
    edited 2009-04-02 06:31
    Thank you Joshua, big oops. Runs great with your suggestions now.
Sign In or Register to comment.