PDA

View Full Version : Help condensing code



snuff28
- 8th July 2012, 05:45
I have now finished my project and it has about 3000 lines of code, probably not written the best way so I am trying to condense some code to pack more features in. Listed below is something I believe could be condensed to save some space.



Lights var PortB.1
LightStatus VAR BYTE
SYMBOL WStatus = LightStatus.0


IF RTCHour = SetlightOnHour THEN
IF RTCMin = SetlightOnMin THEN
HIGH Lights
WStatus = Lights
WRITE 95, WStatus
ENDIF
ENDIF

IF RTCHour = SetlightOffHour THEN
IF RTCMin = SetlightOffMin THEN
LOW Lights
WStatus = Lights
WRITE 95, WStatus
ENDIF
ENDIF

I want to turn a light on and off at a certain time, then write the value of that port.X to the eeprom to save it incase of power failure but only want to write to the eeprom whenever the port changes state.
Hope someone can point me in the right direction.
Thanks!

aerostar
- 9th July 2012, 15:44
How about reading the value at 95 before writing, and if it is the same as Lights then do not do the write.

Perhaps another suggestion convert your time to all minutes (word) then you only need one test instead of 2.

sayzer
- 10th July 2012, 14:12
Lights var PortB.1
LightStatus VAR BYTE
SYMBOL WStatus = LightStatus.0
ChkLights var bit


WStatus = 0
ChkLights = lights ' Take status.

IF RTCHour = SetlightOnHour THEN
IF RTCMin = SetlightOnMin THEN
Lights = 1
WStatus = 1 ' set flag for write.
ENDIF
ENDIF

IF RTCHour = SetlightOffHour THEN
IF RTCMin = SetlightOffMin THEN
Lights = 0
WStatus = 1 ' set flag for write.
ENDIF
ENDIF

if WStatus = 1 then ' Flag is set
if chklights <> lights then ' and lights pin changed its status; so write.
WRITE 95, Lights ' This way, writing to eeprom for entire 1 minute duration is avoided.
WStatus = 0 ' clear flag.
endif
endif

snuff28
- 11th July 2012, 03:21
Thanks aerostar and sayzer!!
I totally overlooked the fact that it would write for the entire minute. That tip is better than condensing the code!!
And it only added 1 byte to the program for 4 lights. Now if I could only get the routines for setting the actual time and the time for each light on/off a lot smaller, right now its like 2000 bytes, but thats for me to work out... or go to a bigger pic.:D

sayzer
- 11th July 2012, 09:38
If you are not using LightStatus.1 anywhere in your program, then

remove "ChkLights var bit" and have this one instead (will save you one byte) : ChkLights var LightStatus.1