;2jan11 usage so far no problems
;30dec10 installed in car
;29dec10 180/256bytes, cleaned code greatly
;29dec10 on time needs increase to 16secs, otherwise ok to date.
;28dec10 ver 10 installed in car to test 2nd ver this date
;27dec10 reduced time to 12seconds,changed intro flash sidelights
;26dec10 ver 9 installed, includes sidelight tester amd exit delay
;2nd version using 08M version 2.0061x ;DEC 2010
;increased fogs threshold to 180 and off to sides plus 50
;changes made to reset variables on ignition off and to have a cumulative
;desensitising with changes over a journey
;program for auto lights/fogs for car.
;pin1 = opto in
;pin2 = sidelights
;Pin3 = ignition
;Pin4 = interior lights
;pin0 = foglights
;b2 = light level hi=dark
;b3 = sidelights dynamic off
;b1 = fogs dynamic off
;b4 = for next loop register
;bit1= exit flag if high
;bit2= ign been on flag if high
;
;---------------------------Start and Ign off top loop-----------------------------------
start:;ign off routine
low 0;fogs go off
low 2;sidelights go off
let b3=50 ;const for lights off setting
readadc 1,b2;read light level to b2
if pin3=0 and pin4=1 and bit1=0 then timer;if ign off and intlt on and bit1=0 then timer
;---------------------------Ign on top loop---------------------------------------------
main: ;ign on routine
if pin4=0 then let bit1=0 endif; if intlt off then bit1=0 this is exit flag
b1=60+b3;b1= threshold at which fogs go off, b3=threshold at which sidelights go off
if pin3 = 0 then goto start; ign off reset line
bit2=1;records the ign has been on, this is ign flag
if b2<b1 then low 0 endif;foglights off threshold level
;multiple samples of light level to ensure no BLIPS in light level throw the outcome
;sampled over a 0.65 second period
;the widened threshold points of 50 and 95 ensure good hysteresis
;-------------------------light sampling loop for sidelights------------------------------
for b4 = 0 to 4;sample check routine
readadc 1,b2;read light level into b2
pause 130
if pin3 = 0 then goto start; ign off reset line
if b2>50 and b2<95 then goto main; if light level in no mans land then no action
next b4
if b2<b3 and pin2 =1 then let pin2 =0 let b3=b3-5min 30 endif;threshold min inc to 30 fm 20
if b2>95 and pin2 =0 then let pin2 =1 endif;lights on threshold
;--------------------------foglights on delay time to prevent flash-----------------------
for b4 = 1 to 5 ; foglights check routine
if b2>220 then high 0 : wait 1 endif;foglight threshold
if pin3 = 0 then goto start;ign off reset line
next b4
if pin2=1 then wait 1 endif;slows down process when lights already on
goto main
;----------------entry/exit routines below-------------------------------
timer:;timer routine if light level is high then skip first part i.e no foglights
;however the exit flag (bit1) is reset even if no illumination occurs
if b2>199 then ret1;if light level low (more than 199) then goto fogson
if bit2=1 or b2<50 then goto ret1;if bit2=0 and lt level dusk then flash sidelights else ret1
;sidelights test routine - only if not exit routine (bit2=0) and b2>80 dusk
;but not higher than 199
for b4 = 0 to 4
high 2
pause 300 ; sidelights test routine
low 2
pause 300
if pin3 = 1 then goto ret; ign on reset line
next b4
ret1: ; routine to filter, if not dark enough then ret(end routine)
;if bit2=1 then must be exit routine
; so the fogs do not come on until the car is locked i.e int lights go off.
if b2<200 or pin3=1 then ret;level at which entry exit fogs activate;if ign on then leave timer
if bit2=0 or pin4=0 then ret2; if ign flag 0 then skip this next routine
goto ret1
ret2: ;foglights on for 15secs unless ign comes on then return
high 0
for b4=0 to 15
wait 1
if pin3 = 1 then goto ret; ign on reset line
next b4
ret: ;exit routine which sets flags and turns lights off (by sending to start)
let bit2=0;reset the ign been on flag
let bit1=1;completed 20sec of fogs on (if dark) still set (exit) flag,
;this prevents repeated cycles if the in light is still on.
goto start