help clean up some code


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838

    Default help clean up some code

    hi guys , this code is rubbish but it looks like it is what i need to do , looking for better ways of writing it

    cheers

    Sheldon
    Code:
        if counter= 1  then 
                 if value0 <> newdata then 
                   value1 = newdata
                   counter = counter +1
                 else 
                    return
                 endif
              endif
             
             if counter = 2 then 
               if value0 <> newdata then
                   if Value1 <> newdata then
                    value2 = newdata 
                    counter = counter +1 
                   else
                    return
                   endif
               endif    
             endif      
          
             if counter = 3 then 
               if value0 <> newdata then
                  if Value1 <> newdata then
                    if Value2 <> newdata then 
                     value3 = newdata 
                     counter = counter +1 
                    else
                     return
                    endif
                 endif    
               endif      
            endif
          
          
           if counter = 4 then 
               if value0 <> newdata then
                  if Value1 <> newdata then
                    if Value2 <> newdata then 
                      if Value3 <> newdata then  
                         value4 = newdata 
                         counter = counter +1 
                      else
                        return
                      endif
                    endif    
                  endif      
               endif
            endif
              
          if counter = 5 then 
               if value0 <> newdata then
                  if Value1 <> newdata then
                    if Value2 <> newdata then 
                      if Value3 <> newdata then  
                        if Value4 <> newdata then
                           value5 = newdata 
                           counter = counter +1 
                        else
                          return
                        endif
                      endif    
                    endif      
                 endif
               endif
           endif     
          if counter = 6 then 
               if value0 <> newdata then
                  if Value1 <> newdata then
                    if Value2 <> newdata then 
                      if Value3 <> newdata then  
                        if Value4 <> newdata then
                           if Value5 <> newdata then 
                               value6 = newdata 
                               counter= counter +1  
                            else
                             return
                           endif
                         endif    
                        endif      
                 endif
               endif
           endif

  2. #2
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    Here's what I'd do:
    if counter = 1 and value0 <> newdata then
    value1 = newdata
    counter = counter +1
    return
    endif
    if counter = 2 and Value0 <> newdata and Value1 <> newdata then
    value2 = newdata
    counter = counter + 1
    return
    endif" .......

    There is probably a clean "select case" application here as well.

    Mike
    Last edited by mpgmike; - 7th April 2014 at 23:31. Reason: code error

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    yes i could do that but i think the actual structure could be improved , the "newdata" and Value0-6 are actully a 3 values tested to see if that table of arrays(49 arrays values) should add to the larger table of arrays with a total of 6 records

    in essence it just test value if it new then add it to the table and inc the counter , if its not then dont add to the table

  4. #4
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    Main:
    select case counter
    case is = 0
    if value0 <> newdata then
    value1 = newdata
    counter = counter +1
    endif
    case is = 1
    if value0 <> newdata and value1 <> new data then
    value2 = newdata
    counter = counter + 1
    endif.......
    return
    Last edited by mpgmike; - 9th April 2014 at 01:27. Reason: it loaded before I was finished

  5. #5
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    thanks mike , both option work ok , as it turned out i decided to go with "and" option , it just really large array group and as such it has about 30 " and " statements per match group of the records it takes about 10k compile for that section , large arrays do take space , i need to write the sorting index routine for this large array in PBP next , not seen any examples where using large data arrays to sort as yet

    cheers

    Sheldon

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    Hi,
    Generally, using nested IF/THENs produces less code than using AND. For example
    Code:
    IF A = 1 THEN
      IF B = 2 THEN
        IF C = 3 THEN
          IF D = 4 THEN
              IF E = 5 THEN
                HIGH PortB.0
              ENDIF
          ENDIF
        ENDIF
      ENDIF
    ENDIF
    Compiled for a 16F628 is 30 words and 76 bytes for 18F4520 (all variables are bytes) while using AND, like this
    Code:
    IF (A = 1) AND (B = 2) AND (C = 3) AND (D = 4) AND (E = 5) THEN
      HIGH PortB.0
    ENDIF
    takes 79 words for the 16F628 and 162 bytes for the 18F4520.

    /Henrik.

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    how does this look ?

    value var byte[6]
    chk var byte

    chknewval:
    chk=0
    while chk<6
    if value[chk] = 0 then
    value[chk]=newvalue
    chk=6
    else
    if value[chk]=newvalue then ;we already have this value
    chk=6
    else
    chk=chk+1
    endif
    endif
    end while
    return ?

  8. #8
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    I just learned something new about PBP! (Use of [brackets]). Thanks Richard!

    Mike

  9. #9
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    i always knew the if , then way produces less words but with 30 "and" compares per test , if then way look really bad and hard to follow, there seems to be no really short way to do large compares for arrays

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    Hi,
    I haven't looked into it in detail but perhaps you could use ARRAYREAD with the WAIT or WAITSTR modifer to do what you want?
    Code:
    Value VAR BYTE[6]   ' Array of 6 bytes for the "new values"
    DummyVAR   'Needed for ArrayWrite to do what we want - possibly
    
    ARRAYREAD Value, 6, NoMatch, [WAIT("123456"), DummyVAR]    ' Search the Value-array for 123456, jump to NoMatch if not found.
    ' Continue here if match is found
    ' ....
    ' ....
    
    NoMatch:
    ' Continue here if no match was found
    The above tries to match the array to a static string of 123456, if what you want to match is changing then use a second array for the "match condition" and use WAITSTR instead of WAIT. At least that's how I THINK it should work, I haven't actually tried it though.....

    /Henrik.

  11. #11
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: help clean up some code

    THANKS HENDRICK , i not looked at that command , will investigate further , i finding it very hard for large arrays and indexing with match , i am working now on arrays using 78 variables per index per group match which 5 variable in the array need to be matched before adding / removing / reindexing , and then needs tobe saved , i have a lot of time before displaying the results on GLCD about 1 sec , i am finding that pbp need to to have more inbuilt functions in this area . iam looking at writing these sort of fuctions in asm , but my asm really need to improve
    space is now becoming an issue

    i may pm if thats ok
    cheers

    sheldon

Similar Threads

  1. Serial problem between BasicStamp code and PBP code
    By AllanZilkowsky in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 6th April 2014, 02:15
  2. Working code but my layman approach uses too much code space
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2012, 20:44
  3. How can I clean up my code?
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th June 2012, 23:34
  4. How to drop from 6V to 5V a "clean" way?
    By flotulopex in forum Off Topic
    Replies: 20
    Last Post: - 10th October 2007, 10:03
  5. Code: Why is this code greater than 2000 words?
    By DrDreas in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 1st June 2007, 19:51

Members who have read this thread : 1

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