Multiple if then optimization


Closed Thread
Results 1 to 36 of 36

Hybrid View

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

    Wink Multiple if then optimization

    Hi Gurus of Picland,

    Iīm building a Lambdatester that flattens somewhat out the hard jump in output voltage between lean and rich.

    wrote a code that works. Fine, and fast enough because theres hardly need for speed.

    Way of simple code for Picdummies like me .

    But if an oldschool mechanic like me looks at it even he knows that theres plenty room for codesaving.
    You Gurus fall down your chairs laughing and crying at the same time, I know.

    '************************************************* ****************
    '* 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 *
    '************************************************* ****************
    '
    ' DEFINITIONS

    @__config _XT_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
    Include "modedefs.bas"
    CMCON=%00000111


    DEFINE OSC 4
    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
    gosub bargraph
    Pause 5
    goto main

    bargraph:

    if Uin < 10 then LCDOUT $FE,1,"TOO LOW"
    if (Uin > 10) and (Uin <= 15 ) then
    LCDOUT $FE,1,255
    endif

    if (Uin > 15) and (Uin <= 19 ) then
    LCDOUT $FE,1,255,255
    endif
    if (Uin > 19) and (Uin <= 24 ) then
    LCDOUT $FE,1,255,255,255
    endif
    if (Uin > 24) and (Uin <= 29 ) then
    LCDOUT $FE,1,255,255,255,255
    endif
    if (Uin > 29) and (Uin <= 34 ) then
    LCDOUT $FE,1,255,255,255,255,255
    endif
    if (Uin > 34) and (Uin <= 43 ) then
    LCDOUT $FE,1,255,255,255,255,255,255
    endif
    if (Uin > 43) and (Uin <= 53 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255
    endif
    if (Uin > 53) and (Uin <= 62 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255
    endif
    if (Uin > 62) and (Uin <= 72 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255
    endif
    if (Uin > 72) and (Uin <= 81 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255
    endif
    if (Uin > 81) and (Uin <= 86 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255,255
    endif
    if (Uin > 86) and (Uin <= 91 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255,255,255
    endif
    if (Uin > 91) and (Uin <= 96 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255,255,255,255
    endif
    if (Uin > 96) and (Uin <= 100 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255,255,255,255,255
    endif
    if (Uin > 100) and(Uin <= 105 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255,255,255,255,255,255
    endif
    if (Uin > 105) and(Uin <= 110 ) then
    LCDOUT $FE,1,255,255,255,255,255,255,255,255,$FE,$C0,255, 255,255,255,255,255,255,255
    endif
    if Uin > 110 then
    LCDOUT $FE,$C0,"TOO RICH"
    endif

    return

    END ' End of program

    on the other hand it works and shows how good and fast the combination of pics and PBP works.

    Any Ideas? maybe case select or lookdown or whatever?

    sincerely

    Mugel

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default

    Hi,

    The Obvious Command is ... SELECT CASE

    Note here you check Twice your values ... a better test arrangement looks like :

    Code:
    IF Value < 19
    ....
    ENDIF
    
    IF Value < 24
    ....
    ENDIF
    
    IF Value < 29
    ....
    ENDIF


    also can be used ...

    Code:
    IF Value < 10 THEN LCDOUT $FE, 1, "TOO LOW" 
    
    LOOKDOWN2 Value , < ,[ 10,15,19,24,29, ....], NbPatterns 
    
    LCDOUT $FE, 1, REP 255\ NbPatterns
    Which is ... Somewhat shorter ( from the listing point of view ...)

    Alain
    ************************************************** ***********************
    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 " !!!
    *****************************************

  3. #3
    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

  4. #4
    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.

  5. #5
    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.

  6. #6
    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

  7. #7
    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.

  8. #8
    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.

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