Multiple if then optimization


Closed Thread
Results 1 to 36 of 36

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Location
    North Norfolk UK
    Posts
    146


    Did you find this post helpful? Yes | No

    Default

    hi Mugel,

    I don't know where all the gurus are today, but in thier absence I glanced at your post and thought you might like to take a look at the >select case< command. As all your conditions are sequentially discrete then this might be worth considering.

    The trouble with long lists of if..thens are that once the condition is met (let say at the begginning of the list) then the program continues to chunder through all the remaining if...thens, whereas select case will exit at >end case< once the condition is met. Be careful though if your conditions are not discrete ,then you will have to arrange the cases in order of preference.

    As to code space, I cannot remember but you could quickly swap the code over and see which is smaller.


    Duncan

  2. #2
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by duncan303 View Post
    The trouble with long lists of if..thens are that once the condition is met (let say at the beginning of the list) then the program continues to chunder through all the remaining if...thens, whereas select case will exit at >end case< once the condition is met. Be careful though if your conditions are not discrete ,then you will have to arrange the cases in order of preference.

    Duncan
    The way around this is to nest them, and put the largest value at the top. For instance, instead of this...

    if a < 5 then...
    endif...
    if a < 10 then...
    endif...

    You do this for nesting:
    if a < 10 then
    if a < 5 then
    code here...
    else
    code here...
    endif
    endif

    or this for simple and fast testing:
    if a < 10 then
    code here...
    goto SkipTests
    endif
    if a < 5
    code here...
    endif

    SkipTests:

    Using this method skips all of the IF's if the most extreme condition isn't true. All of this depends upon the assumption that the value of "a" isn't changed within any of the conditional statements.

  3. #3
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default 1 old mechanic to another

    Hi Mugelpower,
    Have you tried Darrel's Bargraph routine?
    http://www.picbasic.co.uk/forum/showthread.php?t=2359
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  4. #4
    Join Date
    Oct 2004
    Location
    North Norfolk UK
    Posts
    146


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by tenaja View Post
    The way around this is to nest them, and put the largest value at the top.

    I am curious if this would compile with the list that Mugel has created, some 20 nested if…then...Else’s on a 16F device?

    Duncan

  5. #5
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    It should. I believe it just uses goto's, no stack use.

  6. #6
    Join Date
    Jan 2008
    Location
    Selm, Germany
    Posts
    116


    Did you find this post helpful? Yes | No

    Cool 20 if then

    yes the 20 if then work, no problem. Assembling took maybe 3 times longer then those blinky codes and it uses nearly 3/5 of the code space in my 16F628..but works flawlessly.

    to Joe:

    oh man I tried to use DTs bargraph routines but only got the error "coulndīt open bargraph.bas" or something. Copied it in a "dozen" directories around the main PBP main and my project directories...didnīt work, always the same error.
    But I uses DTs instant interrupt blinky program and that worked....so I donīt know a bone about how to work around "include" problems.

    On the other my bargraph is quite crude with its big bars....but a fine bargraph isnīt an option here...way to hard to see while driving the car or bike, you need only sort of lean/rich tendencies and the lambda sensor it VERY jumpy around the stochiometric area.

    8 leds would do but I did it mainly because of 3 reasons:

    1.: I COULD do it.......with a little help of my friends....greetings to you all!
    2.: One of my employees is sort of colour blind and wanted something to recocnize
    without colours.
    3.: we do a lot around sportscars engines and we test drive them mainly when the sun shines and a LED bargraph is hard to read while driving 100km/h in corners and the sun shining all over....

    Yeah......Iīm telling junk. Here in Germany the sunīs just a rumour you know, seen last time in june 2007 or so....
    Last edited by Mugelpower; - 3rd March 2008 at 07:13.

  7. #7
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Here it is
    982 words

    Code:
    <font color="#008000">@__config _XT_OSC &amp; _WDT_ON &amp; _MCLRE_ON &amp; _LVP_OFF &amp; _CP_OFF
    
    </font><font color="#000080"><b>INCLUDE </b></font><font color="#008000"><b>&quot;modedefs.bas&quot;
    </b></font><font color="#000080"><b>DEFINE </b></font>OSC <font color="#FF0000"><b>4
    </b></font>Uin        <font color="#000080"><b>VAR WORD
    </b></font>B0         <font color="#000080"><b>VAR WORD </b><i>' Cnt is a word variable
    </i></font>InputData  <font color="#000080"><b>VAR WORD </b><i>' variable to receive data into
    </i></font>RepNo      <font color="#000080"><b>VAR BYTE
    
    </b><i>' START OF MAIN PROGRAM
    '
    </i></font>CMCON = <font color="#FF0000"><b>7 </b></font><font color="#000080"><i>' RA0-RA3 are digital I/O
    </i></font>TRISA = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' PORT A is output
    </i></font>TRISB = <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' RB0 is Input others output
    
    </i></font>Main:
    
         <font color="#000080"><b>SERIN </b></font>PORTB.<font color="#FF0000"><b>0</b></font>,N2400,B0
         B0 = <font color="#FF0000"><b>100 </b></font>* B0
         Uin = B0 / <font color="#FF0000"><b>207
         </b></font><font color="#000080"><b>GOSUB </b></font>Bargraph
         <font color="#000080"><b>PAUSE </b></font><font color="#FF0000"><b>5
    
    </b></font><font color="#000080"><b>GOTO </b></font>Main
    
    Bargraph:
    
    <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$fe</b></font>,<font color="#FF0000"><b>1      </b></font><font color="#000080"><i>'Valid for all cases.
    
    </i><b>SELECT CASE </b></font>Uin
           
     <font color="#000080"><b>CASE IS </b></font>&lt; <font color="#FF0000"><b>10 
          </b></font><font color="#000080"><b>LCDOUT </b></font><font color="#008000"><b>&quot;TOO LOW&quot;
     </b></font><font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>10 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>15
          </b></font>RepNo = <font color="#FF0000"><b>1
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>15 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>19
          </b></font>RepNo = <font color="#FF0000"><b>2
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>19 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>24
          </b></font>RepNo = <font color="#FF0000"><b>3
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>24 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>29
          </b></font>RepNo = <font color="#FF0000"><b>4
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>29 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>34
          </b></font>RepNo = <font color="#FF0000"><b>5
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>34 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>43
          </b></font>RepNo = <font color="#FF0000"><b>6
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>43 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>53
          </b></font>RepNo = <font color="#FF0000"><b>7
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>53 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>62 
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>62 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>72
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>1      
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>72 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>81
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>2
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>81 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>86
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>3
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>86 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>91
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>4
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>91 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>96
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>5
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>96 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>100
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>6
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>100 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>105
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>7
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE IS </b></font>&gt; <font color="#FF0000"><b>105 </b></font><font color="#000080"><b>AND IS </b></font>&lt;= <font color="#FF0000"><b>110
          </b></font>RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
          <font color="#000080"><b>GOSUB </b></font>LCD2
          RepNo = <font color="#FF0000"><b>8
          </b></font><font color="#000080"><b>GOSUB </b></font>RepLCD
    
     <font color="#000080"><b>CASE ELSE 
          LCDOUT </b></font><font color="#FF0000"><b>$FE</b></font>,<font color="#FF0000"><b>$C0</b></font>,<font color="#008000"><b>&quot;TOO RICH&quot;
    </b></font><font color="#000080"><b>END SELECT
    
    RETURN
    
    </b></font>RepLCD:
        <font color="#000080"><b>WHILE </b></font>RepNo &gt; <font color="#FF0000"><b>0
              </b></font><font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>255
              </b></font>RepNo = RepNo - <font color="#FF0000"><b>1
        </b></font><font color="#000080"><b>WEND
       </b></font><font color="#000080"><b>RETURN
    
    </b></font>LCD2:
        <font color="#000080"><b>LCDOUT </b></font><font color="#FF0000"><b>$FE</b></font>,<font color="#FF0000"><b>$C0
        </b></font><font color="#000080"><b>RETURN
        
    </b></font>

    Btw:
    Select case produces 100 words more!
    So, if you go with your IF..THENs, it will be approx. 882 words, but select case provides a clean look.
    Last edited by sayzer; - 3rd March 2008 at 16:11.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Talking ....

    Another way

    Code:
    '************************************************* ****************
    '* Name : Analogbargraph1(16F628A).BAS *
    '* Author : M.David *
    '* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 28.02.2008 *
    '* Version : 1.0 *
    '* Notes : needs 12F675 for Lambdatesting device and LCD 1X16 *
    '* : input 0.1 - 1.1 Volts in GPIO.0 *
    '* : needs ADCserout(12F675).bas *
    '  569 Lines
    '************************************************* ****************
    '
    ' DEFINITIONS
    
    @      __config _XT_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
    Include "modedefs.bas"
    'CMCON=%00000111
    
    
    DEFINE OSC 4
    
    NbPatterns	var Byte
    Uin 		var word
    B0 			VAR word ' Cnt is a word variable
    inputData	var word ' variable to receive data into
    
    
    ' START OF MAIN PROGRAM
    '
    CMCON = 7 ' RA0-RA3 are digital I/O
    TRISA = 0 ' PORT A is output
    TRISB = 1 ' RB0 is Input others output
    
    main:
    
    Serin PORTB.0,N2400,B0 
    B0 = 100 * B0
    Uin = B0 / 207
    
    bargraph:
    
    IF Uin < 10 THEN LCDOUT $FE,1,"TOO LOW" : GOTO Show
    
    IF Uin > 110 THEN LCDOUT $FE,1,"TOO HIGH" : GOTO Show
    
    
    LOOKDOWN2 Uin , < [ 11,16,20,25,30,35,44,54,63], NbPatterns 
    
    LCDOUT $FE, 1, REP 255\ NbPatterns
    
    IF Uin > 62 THEN
    
    LOOKDOWN2 Uin , < [ 63,73,82,87,92,97,101,106,111], NbPatterns 
    
    LCDOUT $FE, 1, REP 255\ 8
    
    LCDOUT $FE, $C0, REP 255\ NbPatterns
    
    ENDIF
    
    Show:
    
    Pause 5 
    goto main
    
    
    END ' End of program



    569 Words ...




    Alain
    Last edited by Acetronics2; - 3rd March 2008 at 09:51. Reason: CMCON : Double definition ....
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sayzer View Post
    CASE IS >19 AND IS <= 24
    Does the AND in the CASE IS actually work?
    I thought the CASE could only take one parameter to work on...

Similar Threads

  1. Multiple PICS from Same Crystal?
    By WOZZY-2010 in forum General
    Replies: 2
    Last Post: - 6th February 2010, 15:18
  2. Multiple analog inputs ?
    By rngd in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th February 2008, 15:13
  3. Multiple PIC programming
    By Nicholas in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 7th May 2007, 23:47
  4. Multiple PIC's with 1 crystal
    By puma in forum Schematics
    Replies: 11
    Last Post: - 20th March 2007, 17:02
  5. Multiple IR LEDs from 1 port using transistor
    By belpe123 in forum General
    Replies: 3
    Last Post: - 20th May 2005, 22:07

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