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