Old classic If-Then...


Closed Thread
Results 1 to 17 of 17
  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,836

    Default Old classic If-Then...

    I got a little confussed on this nested If-Then:

    Code:
    if level<=high_level then
    	if thermiko1=0 then
    		if thermistor1=0 then
    			if auto1=0 then
    				if onboard_an2<=pres_limit then
    					if pump=0 then
    						module_rel_out=1
    					else
    						module_rel_out=0
    					endif
    				endif
    			endif
    		endif
    	endif
    endif
    does make module_rel_out=1 but does not makes it 0 if the conditions (one or more) are not met.

    I am missing something but cannot figure what...

    Any help appreciated.
    Ioannis

  2. #2


    Did you find this post helpful? Yes | No

    Default

    You need to insert the ELSE statement for each IF. For example, if the first IF is not true, the code never gets to the ELSE ...=0

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    to execute this part...
    Code:
    if pump=0 then
    						module_rel_out=1
    					else
    						module_rel_out=0
    					endif
    ALL previous conditions have to be met.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    Simply pre-load the ELSE condition into the variable before entering the IF loop...
    Code:
    module_rel_out=0
    if level<=high_level then
    	if thermiko1=0 then
    		if thermistor1=0 then
    			if auto1=0 then
    				if onboard_an2<=pres_limit then
    					if pump=0 then module_rel_out=1
    					endif
    				endif
    			endif
    		endif
    	endif

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Ioannis

    Why not use an AND combination of your conditions.

    Karnaugh would LOVE your example.

    Try this little soft ... and ENJOY.

    Karnaugh Minimizer 2.0
    Copyright (c) 2002-2007 ShurikSoft.
    http://www.shuriksoft.com

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

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,836


    Did you find this post helpful? Yes | No

    Default

    Hi all and thanks for the replies.

    Alain: I tried with AND but got an error of too many variables. Indeed was long line!

    Melanie: That was what I did just before reading your post. Yes this works but was not appealing to me as an elegant solution.

    Steve & Tobias: Yes,I see now, after the morning cup of coffee that it never reached the ELSE statement.

    Thanks again to all.

    Ioannis

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi Ioannis

    I do not Understand you ...

    Code:
    'Essai 16F84 Random
    
    DEFINE LCD_EBIT 1                          ' Other LCD pins are same as Manual - Holy Manual - my sweet Manual ...
    
    '   DEFINE LCD_DREG PORTB               ' I/O port where LCD is connected
    '   DEFINE LCD_DBIT 0
    '   DEFINE LCD_RSREG PORTB
    '   DEFINE LCD_RSBIT 4                  ' Register select pin
    '   DEFINE LCD_EREG PORTB
    '   DEFINE LCD_EBIT 5                   ' Enable pin
    '   DEFINE LCD_BITS 4                   ' 4-bit data bus
    '   DEFINE LCD_LINES 2                  ' LCD has 2 character lines
     
       DEFINE OSC 4
       
    @	__config _XT_OSC & _WDT_ON & _CP_OFF
    
    '*****************************************************************************
    'Variables
    
    Level 	   		var Word
    High_Level 		var Word
    
    Onboard_an2		var Word
    PressLimit 		var Word
    
    Auto1			var Bit
    Thermikol 		var Bit
    Thermistor1		var Bit
    Pump			var Bit
    
    LevelOk			var Bit
    PressOk			var Bit
    
    module_rel_out	var Portb.7
    
    '*****************************************************************************
    'Constantes
    
    '*****************************************************************************
    'Preset
    
    CLEAR
    
    PORTA = 0
    PORTB = 0
    
    TRISA = 0
    TRISB = 0
    
    High_Level 	= 40000
    Level		= 0
    Thermikol   = 1
    Thermistor1	= 1
    Onboard_an2 = 500
    PressLimit 	= 35000
    
    Auto1		= 1
    Pump		= 0
    
    
    
    PAUSE 500
    
    LCDOUT $FE,1
    
    RANDOM Level
    RANDOM Onboard_An2
    
    Loop:
    
    RANDOM Level
    RANDOM Onboard_An2
    
    IF (level <= high_Level) THEN
     	PORTB.5 = 1
    ELSE
      	PORTB.5 = 0
    ENDIF
    
    IF ( onboard_an2 <= PressLimit ) THEN
     	PORTB.6 = 1
    ELSE
      	PORTB.6 = 0
    ENDIF
    
    Module_rel_out =  (level <= high_Level) && Thermikol && Thermistor1 && Auto1 && ( onboard_an2 <= PressLimit ) && !Pump  
    
    LCDOUT $FE,2,# Module_Rel_Out ,"   ",DEC5 Level ," ",DEC5 Onboard_an2 ,"  "
    LCDOUT $FE,$C0,"out", DEC5 High_Level, " ", DEC5 Presslimit 
    
    PAUSE 300
    
    IF Module_rel_out THEN PAUSE 500
    
    GOTO Loop
    
    END
    As you see ... I played somewhat with my Personnal Testboard to verify everything was running fine ...

    And it runs fine !!!

    "your" line is here :

    Code:
    Module_rel_out =  (level <= high_Level) && Thermikol && Thermistor1 && Auto1 && ( onboard_an2 <= PressLimit ) && !Pump
    ...

    Value Comparisons or logic levels have equal behaviour here ...

    Cheers ...

    Alain
    Last edited by Acetronics2; - 1st November 2008 at 14:33.
    ************************************************** ***********************
    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 " !!!
    *****************************************

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,836


    Did you find this post helpful? Yes | No

    Default

    Hi Alain.

    On the following if I try to put the second if-then inside the logic expression, I get an "Temp variables exceeding T4" error in MPASM assembler.

    This compiles OK:
    Code:
    if pump=0 and (thermistor1=1 or thermiko1=1 or onboard_an2>(pres_limit+10)) then 
    	module_rel_out.0=0
    endif
    
    
    if level>stop_level then
    	if pump=0 then
    		module_rel_out.0=0
    	endif
    endif
    This produces the above error:

    Code:
    if pump=0 and (thermistor1=1 or thermiko1=1 or onboard_an2>(pres_limit+10) or level>stop_level) then 
    	module_rel_out.0=0
    endif
    Thanks,
    Ioannis

  9. #9
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Hi, Ioannis

    Do not use " IF thermistor1 = 1" but "IF Thermistor" ... result is the same

    Do not use " IF thermistor1 = 0 " but "IF NOT Thermistor" ... result is the same

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

  10. #10
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Wink

    Code:
    if  (pump=0) and ( thermistor1 or thermiko1 or ( onboard_an2>(pres_limit + 10))) then 
    	module_rel_out.0=0
    endif
    
    
    if (level>stop_level) then
    	if pump=0 then
    		module_rel_out.0=0
    	endif
    endif
    Compile fine

    BUUUUUUT ... I do think you should make your Karnaugh Table ( Table of states ) "a bit" clearer ... might help a lot ( lol )

    Alain

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

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


    Did you find this post helpful? Yes | No

    Default

    Thanks Alain,
    For
    Code:
    RANDOM Level
    RANDOM Onboard_An2
    It gives me a new test tool, never could see a use for that command before. Sometimes it's not the main point of your posting that benefits others, sometimes it's the little things that you use everyday.
    Last edited by Archangel; - 1st November 2008 at 20:19.
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi, Ioannis

    Do not use " IF thermistor1 = 1" but "IF Thermistor" ... result is the same

    Do not use " IF thermistor1 = 0 " but "IF NOT Thermistor" ... result is the same

    Alain
    Could you please explain a little more, so I can understand why ?
    Thank You
    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.

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I understand why.. the problem is to explain... enjoy the following language barrier mess

    If ConditionIsTrue Then DoSomething

    Dealing with Bit Var, you have ONLY 2 choices... OR the var is 1 OR the var 0, True or False.

    So why messing with condition if your variable already hold it?

    It's just another way to write your IF-THEN
    Last edited by mister_e; - 1st November 2008 at 20:41.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    I get an "Temp variables exceeding T4" error in MPASM assembler.
    Hi Ioannis,

    That error is comming from ReEnterPBP.bas

    There's no way for it to know if the complex formula/IF statement is in the main program or the interrupt handler. And if it's in the handler, it will end up overwriting PBP's system vars.

    If you know for sure that the offending statements are NOT in the handler. You can open the ReEnterPBP.bas file and comment the error line.
    Code:
            ifdef T5
    ;            ERROR "Temp variables exceeding T4"
            endif
    HTH,
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Hi Ioannis,

    That error is comming from ReEnterPBP.bas

    There's no way for it to know if the complex formula/IF statement is in the main program or the interrupt handler. And if it's in the handler, it will end up overwriting PBP's system vars.

    If you know for sure that the offending statements are NOT in the handler. You can open the ReEnterPBP.bas file and comment the error line.
    Code:
            ifdef T5
    ;            ERROR "Temp variables exceeding T4"
            endif
    HTH,
    Hi Darrel,
    So if I understand this right, ERROR is a reserved word in assembler, and if IFDEF calls it then MPASM displays the error when compiling . . . YES?<br><br> Hi Steve, thanks , I will give that some thought.
    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.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Joe S. View Post
    Hi Darrel,
    So if I understand this right, ERROR is a reserved word in assembler, and if IFDEF calls it then MPASM displays the error when compiling . . . YES?
    Yup. You got it.
    DT

  17. #17
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,836


    Did you find this post helpful? Yes | No

    Default

    Hi. Thanks for the replies.

    Alain: I was going to use the syntax later (if thermistor then...), after I had my program working. But as Darrel explained, this would have no effect as the error was coming from the ReEnterPBP.bas.

    Darrel: Thanks for the explanation. I forgot to mention that I was using yours routine for the Interupts, sorry... But did not imagine that this could be coming from the ReEnterPBP.bas. It seemed more logical to be a problem of the Assembler or Compiler.

    Useful the tip for the error.

    Anyway, many thanks to all.

    Ioannis

Similar Threads

  1. Classic
    By Haggar in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th April 2005, 09:57

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