PDA

View Full Version : Re-Writing IF-THEN-AND-ENDIF code?



jessey
- 16th August 2006, 09:26
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



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




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

paul borgmeier
- 16th August 2006, 15:51
Although I have not dove into the logic, this could be part of your issues ...

Darrel's Code Snippet

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


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

Archilochus
- 16th August 2006, 16:41
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...



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

jessey
- 17th August 2006, 06:17
Hi Paul,



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



' 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




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.



' 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

mister_e
- 17th August 2006, 06:24
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!

SteveB
- 17th August 2006, 20:18
Jessey,
Here is a small refinement to your IF-THEN-ENDIF


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

Archilochus
- 18th August 2006, 17:23
Here is a small refinement to your IF-THEN-ENDIF


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



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