Array and IF - THEN


Closed Thread
Results 1 to 10 of 10

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191

    Post Array and IF - THEN

    I tried to search info about how to handle input strings (arrays) and do selections based on what was received to an array, but not so good success. Most probably I don't know what are the right key words..., but anyway.
    Below is sample code, which is very similar that I have in the program. Only difference is that I use 6 bytes instead of 4. Code is working OK.

    Question is, can I do this more sophisticated way?
    I have lots of similar IF - THEN and I really want to know if there would be a more simple way to do this.

    Code:
    <code><font color="#000000">LED         <b>VAR </b>PortB.0
    Data_Array  <b>VAR BYTE </b>[4]
    
    Mainloop:
        <b>DEBUGIN </b>1000, Mainloop, [<b>STR </b>Data_Array\4]
        <b>IF </b>Rele_Array[0] = <font color="#FF0000">&quot;A&quot; </font><b>THEN
            IF </b>Rele_Array[1] = <font color="#FF0000">&quot;B&quot; </font><b>THEN
                IF </b>Rele_Array[2] = <font color="#FF0000">&quot;C&quot; </font><b>THEN
                    IF </b>Rele_Array[3] = <font color="#FF0000">&quot;1&quot; </font><b>THEN
                        HIGH </b>LED                    <font color="#000080"><i>'If received ABC1
                        </i></font><b>PAUSE </b>1000                  <font color="#000080"><i>'then turn LED on
                        </i></font><b>LOW </b>LED                     <font color="#000080"><i>'for 1s and return
                        </i></font><b>GOTO </b>Mainloop               <font color="#000080"><i>'to Mainloop
                    </i></font><b>ENDIF
                    IF </b>Rele_Array[3] = <font color="#FF0000">&quot;2&quot; </font><b>THEN
                        HIGH </b>LED                    <font color="#000080"><i>'If received ABC2
                        </i></font><b>PAUSE </b>2000                  <font color="#000080"><i>'then turn LED on
                        </i></font><b>LOW </b>LED                     <font color="#000080"><i>'for 2s and return
                        </i></font><b>GOTO </b>Mainloop               <font color="#000080"><i>'to Mainloop
                    </i></font><b>ENDIF
                ENDIF
            ENDIF
        ENDIF
    
        END
    
    </b></code>
    BR,
    -Gusse-
    Last edited by Gusse; - 22nd January 2010 at 18:54.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Code:
    LED VAR PortB.0
    Dat VAR BYTE
    
    Mainloop:
        DEBUGIN 1000, Mainloop, [WAIT("ABC"),Dat]
        Dat=Dat-48 
        HIGH LED            
        PAUSE Dat * 1000
        LOW LED
        GOTO Mainloop      
        END
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Thanks Bruce, but what if I have several different prefixes?

    1) ABC1, ABC2, etc...
    2) DEF1, DEF2, etc...
    3) GHI1, GHI2, etc...
    4) etc...

    I cannot wait only "ABC", but basically whatever I might have in the code.

    BR,
    -Gusse-
    Last edited by Gusse; - 22nd January 2010 at 21:05.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    If you have several different prefixes, then you're stuck checking the 1st 3 bytes
    pretty much like in your original example.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post

    Thanks Bruce!

    I'll continue to use IF - THENs. This is probably something that could be placed to "PBP Wish List" if it would be benefit also someone else.

    BR,
    -Gusse-

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    If you have PBP 2.60, you might do something like this ...
    Code:
    LED         VAR PortB.0
    CmdIdx      VAR BYTE
    Data_Array  VAR BYTE[4]
    CmdString   VAR BYTE[3]
    
    ;--- Subroutine to parse commands -----------------------------
    Parse:
        FOR CmdIdx = 0 to 3
            SELECT CASE CmdIdx
              CASE 0 : ARRAYWRITE CmdString,["ABC"]  
              CASE 1 : ARRAYWRITE CmdString,["DEF"]  
              CASE 2 : ARRAYWRITE CmdString,["GHI"]  
              CASE 3 : ARRAYWRITE CmdString,["JKL"]  
            END SELECT
            ARRAYREAD  Data_Array,3,NoCmd,[WAITSTR CmdString\3]
            ON CmdIdx GOTO cmd_ABC, cmd_DEF, cmd_GHI, cmd_JKL ; found cmd
          NoCmd:  
        NEXT CmdIdx
        ; if it gets here no commands were found
    RETURN
    
    cmd_ABC:
         SELECT CASE Data_Array[3]
           CASE "1"
                HIGH LED                    'If received ABC1
                PAUSE 1000                  'then turn LED on
                LOW LED                     'for 1s and return
           CASE "2"
                HIGH LED                    'If received ABC2
                PAUSE 2000                  'then turn LED on
                LOW LED                     'for 2s and return
         END SELECT
    RETURN
    
    cmd_DEF: 
    
    RETURN
    
    cmd_GHI:
    
    RETURN
    
    cmd_JKL:
    
    RETURN
    DT

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts