Re-Writing IF-THEN-AND-ENDIF code?


Closed Thread
Results 1 to 7 of 7
  1. #1
    jessey's Avatar
    jessey Guest

    Default Re-Writing IF-THEN-AND-ENDIF code?

    Hello,

    I wrote a timer program using Darrels
    EE_Vars.PBP
    DT_INTS-18.bas
    ReEnterPBP-18.bas
    Elapsed_INT-18.bas
    and my program works great until I decided to try and save some code space by re-writing some of the code where I used IF-THEN-AND-ENDIF's. I can't seem to convert the code segment below to get it to work, as re-written the alarm won't sound for some reason. I'm using a PIC18f452-20/P. Its really starting to bug me why I can't figure it out as it seems pretty simple and straight forward enough?

    Does anyone have any ideas on what I did wrong?

    Thanks
    jessey

    Code:
    This code works good.....
    
    'This is the alarm check. The first IF-THEN here 
    'prevents the alarm from sounding when the timer 
    'set points aren't set, as it will be on the 
    'initial power-up or when both resets buttons 
    'have been pressed..............................
    '==============================================='
    
    IF Seconds = 0 AND Minutes = 0 AND Hours = 0 AND _
       Seconds_Alarm_Set_Point = 0 AND _
       Minutes_Alarm_Set_Point = 0 AND _
        Hours_Alarm_Set_Point = 0 THEN  
    ELSE 'If above code is true then don't check the code below 
     IF Start_Stop_Mode = StartTime THEN 'if the clock is running    
      IF Seconds = Seconds_Alarm_Set_Point AND _
         Minutes = Minutes_Alarm_Set_Point AND _
            Hours = Hours_Alarm_Set_Point THEN
        GOSUB StopTimer : LOW Clock_Set_Green_Led 
        Alarm_Is = On_
      ENDIF
     ENDIF
    ENDIF
    Code:
    This converted code from above doesn't work.....
    
    IF Seconds = 0 THEN 
     IF Minutes = 0 THEN 
      IF Hours = 0 THEN
       IF Seconds_Alarm_Set_Point = 0 THEN
        IF Minutes_Alarm_Set_Point = 0 THEN
         IF Hours_Alarm_Set_Point = 0 then
         ELSE 'If above code is true then don't check the code below
          IF Start_Stop_Mode = StartTime THEN 'if the clock is running   
            IF Hours = Hours_Alarm_Set_Point THEN
             IF Minutes = Minutes_Alarm_Set_Point THEN
              IF Seconds = Seconds_Alarm_Set_Point THEN
                GOSUB StopTimer
                LOW Clock_Set_Green_Led : Alarm_Is = On_  
              ENDIF
             ENDIF    
            ENDIF
          ENDIF
         ENDIF
        ENDIF
       ENDIF
      ENDIF
     ENDIF
    ENDIF

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Although I have not dove into the logic, this could be part of your issues ...

    Darrel's Code Snippet
    Code:
    IF Seconds = 0 AND Minutes = 0 AND Hours = 0 AND _
       Seconds_Alarm_Set_Point = 0 AND _
       Minutes_Alarm_Set_Point = 0 AND _
        Hours_Alarm_Set_Point = 0 THEN  
    ELSE 'If above code is true then don't check the code below 
     ...
    Darrel's code goes to "ELSE" if any of the above are not true

    Your Code Snippet
    Code:
    IF Seconds = 0 THEN 
     IF Minutes = 0 THEN 
      IF Hours = 0 THEN
       IF Seconds_Alarm_Set_Point = 0 THEN
        IF Minutes_Alarm_Set_Point = 0 THEN
         IF Hours_Alarm_Set_Point = 0 then
         ELSE 'If above code is true then don't check the code below
     ...
    Yours goes to ELSE only if Hours_Alarm_Set_Point =0 is not true, otherwise they go to ENDIF and out of the massive IF-THEN jungle
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  3. #3
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Another way to do it that might work...
    Since you're checking to see that all those variables = 0, you could just first add them all (making sure that the Temp variable size won't be exceeded), then just test the sum...

    Code:
    Temp = Seconds + Minutes + Hours + Seconds_Alarm_Set_Point + Minutes_Alarm_Set_Point + Hours_Alarm_Set_Point
    
    IF Temp=0 THEN  ; test to see if Temp is 0
    ;  do something
    ;  etc, etc
    Arch
    "Data sheets? I ain't got no data sheets. I don't need no data sheets. I don't have to read any stinking data sheets!"

  4. #4
    jessey's Avatar
    jessey Guest


    Did you find this post helpful? Yes | No

    Default Great answers..... thanks

    Hi Paul,

    Quote Originally Posted by paul borgmeier
    Yours goes to ELSE only if Hours_Alarm_Set_Point =0 is not true, otherwise they go to ENDIF and out of the massive IF-THEN jungle
    Your right on that point Paul, I re-thought it out and came up with this code below and it works......... thanks

    Code:
    '  This saves 142 bytes
    '  ==================== 
    
    F VAR BIT 
    F = 0     
    IF Seconds = 0 THEN F = 1 
    IF Minutes = 0 THEN F = 1 
    IF Hours = 0 THEN F = 1
    IF Seconds_Alarm_Set_Point = 0 THEN F = 1
    IF Minutes_Alarm_Set_Point = 0 THEN F = 1
    IF Hours_Alarm_Set_Point = 0 then F = 1
      IF F = 0 THEN
      ELSE
       IF Start_Stop_Mode = StartTime THEN   
        IF Hours = Hours_Alarm_Set_Point THEN
         IF Minutes = Minutes_Alarm_Set_Point THEN
          IF Seconds = Seconds_Alarm_Set_Point THEN
             GOSUB StopTimer
             LOW Clock_Active_Green_Led : Alarm_Is = On_  
          ENDIF
         ENDIF    
        ENDIF
       ENDIF
      ENDIF
    Quote Originally Posted by Archilochus
    Another way to do it that might work...
    Since you're checking to see that all those variables = 0, you could just first add them all (making sure that the Temp variable size won't be exceeded), then just test the sum...
    That's a really neat efficient approach Arch and its the one I'll use. I would have never thought of that way of doing it but now I'll never forget it, its one more tool in my tool box now! Thanks a lot guys, I really appreciate your input.

    Code:
    '  This saves 182 bytes
    '  ====================
    
      Temp = Seconds + Minutes + Hours + Seconds_Alarm_Set_Point + _
                   Minutes_Alarm_Set_Point + Hours_Alarm_Set_Point
      IF Temp = 0 THEN
      ELSE
       IF Start_Stop_Mode = StartTime THEN   
        IF Hours = Hours_Alarm_Set_Point THEN
         IF Minutes = Minutes_Alarm_Set_Point THEN
          IF Seconds = Seconds_Alarm_Set_Point THEN
            GOSUB StopTimer
            LOW Clock_Active_Green_Led : Alarm_Is = On_  
          ENDIF
         ENDIF    
        ENDIF
       ENDIF
      ENDIF
    Thanks
    jessey

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


    Did you find this post helpful? Yes | No

    Default

    EDIT: Nevermind.. i misread the Arch post... don't forget to use a Word sized variable is the only thing you need to remember.

    Good night!
    Last edited by mister_e; - 17th August 2006 at 07:30.
    Steve

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

  6. #6
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default

    Jessey,
    Here is a small refinement to your IF-THEN-ENDIF
    Code:
      Temp = Seconds + Minutes + Hours + Seconds_Alarm_Set_Point + _
                   Minutes_Alarm_Set_Point + Hours_Alarm_Set_Point
      IF Temp <> 0 THEN  'Not Equal to 0
                         '<- Eliminates the unused ELSE
          IF Start_Stop_Mode = StartTime THEN   
              IF Hours = Hours_Alarm_Set_Point THEN
                  IF Minutes = Minutes_Alarm_Set_Point THEN
                      IF Seconds = Seconds_Alarm_Set_Point THEN
                          GOSUB StopTimer
                          LOW Clock_Active_Green_Led : Alarm_Is = On_  
                       ENDIF
                  ENDIF    
              ENDIF
         ENDIF
      ENDIF
    Steve

  7. #7
    Join Date
    May 2004
    Location
    New England
    Posts
    164


    Did you find this post helpful? Yes | No

    Default

    Here is a small refinement to your IF-THEN-ENDIF
    Code:
      Temp = Seconds + Minutes + Hours + Seconds_Alarm_Set_Point + _
                   Minutes_Alarm_Set_Point + Hours_Alarm_Set_Point
      IF Temp <> 0 THEN  'Not Equal to 0
    
      ENDIF


    Hmmm...

    Maybe could go to this too...

    Code:
    IF Temp THEN ; Any Temp value over zero will make the "IF" true
     ;do something
    EndIF
    This may not work with older versions of PBP.

    Arch
    "Data sheets? I ain't got no data sheets. I don't need no data sheets. I don't have to read any stinking data sheets!"

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