'This code is for a smart outdoor light unprofessionally
'uses a 20M2 chip and reads a ds1307 real time clock
'also has a microwave sensor or a PIR sensor on pin b.1
'also will control a SSR(solid state relay) on pin b.0
setfreq m4
input b.1 ' motion sensor switch
input b.2 ' switch for lights on all nite (optional)
SYMBOL index = b0 'w0
SYMBOL hours = b1 'w0
SYMBOL mins = b2 'w1
SYMBOL secs = b3 'w1
SYMBOL day = b4 'w2
SYMBOL date = b5 'w2
SYMBOL month = b6 'w3
SYMBOL year = b7 'w3
SYMBOL CommonYear = b8 'w4
symbol counter = b9 'w4 variable used during loop
symbol mask = w5 'b10-b11 bit masking variable
'symbol mask word = b11
symbol var_out = w6 'b12-b13‘ data
'symbol var_out = b13 'b12-b13‘ data
SYMBOL DayNumber = W7
'SYMBOL DayNumber = b15 b14-b15‘ data
SYMBOL DSTstart = W8
SYMBOL DayS = W8 '= b17 b16-b17‘ data
SYMBOL DSTend = W9
'SYMBOL DSTend = b19 b18-b19‘ data
symbol on_off_hour = b20
symbol loopone = b21
symbol Ltime = b22
'symbol = b23
'symbol = b24
'symbol = b25
'symbol = b26
'symbol = b27
Ltime = 1
'InitialiseTime:
'HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte ' Set DS1307 to 100kbps
'HI2Cout $0 , ( $10, $56, $14,day, $25 , $08 , $17) ' program DS1307 with GMT
' sec min hrs dte mnth year
START:
DO
If pinb.2 = 1 then gosub always_on 'incase a person wants it always on during night
gosub gettime
gosub caltime
pause 6000
gosub lighton_off
LOOP
GETTIME: 'thanks to MARKS for the gettime code below
HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte ' Set DS1307 to 100kbps
HI2Cin $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307
FOR bptr = 1 TO 6
@bptr = @bptr/$10*250+@bptr ' Convert BCD to Decimal
NEXT
CommonYear = year //4 +3 /4 ' CommonYear =1
DayNumber = month +9 /12
DayNumber = CommonYear + DayNumber * DayNumber
DayNumber = month *275 /9 +date -30 -DayNumber
DayS = year *512 **46752 ' year =1 to 99
DSTstart = Days +72//7
DSTstart = 74 -CommonYear -DSTstart ' Second SunDay in March
DSTend = DSTstart +238 ' First SunDay in November
IF DayNumber < DSTstart OR DayNumber > DSTend THEN caltime
IF DayNumber = DSTstart AND hours <2 THEN caltime ' DST starts at 0200
IF DayNumber = DSTend AND hours >1 THEN caltime ' DST ends at 0200
IF hours =23 THEN : INC date
IF month =4 or month =6 or month =9 and date =31 OR date=32 THEN : date =1 INC month: ENDIF
ENDIF : hours =hours +1//24
RETURN
CALTIME:
If month = 1 then 'january
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,0,1,1,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 2 then 'february
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,0,1,1,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 3 then 'march
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,0,0,1,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 4 then 'april
LOOKUP hours, (1,2,2,2,2,2,1,0,0,0,00,00,00,0,0,0,0,0,0,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 5 then 'may
LOOKUP hours, (1,2,2,2,2,2,1,0,0,0,00,00,00,0,0,0,0,0,0,0,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 6 then 'june
LOOKUP hours, (1,2,2,2,2,1,0,0,0,0,00,00,00,0,0,0,0,0,0,0,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 7 then 'july
LOOKUP hours, (1,2,2,2,2,1,0,0,0,0,00,00,00,0,0,0,0,0,0,0,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 8 then 'august
LOOKUP hours, (1,2,2,2,2,2,1,0,0,0,00,00,00,0,0,0,0,0,0,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 9 then 'september
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,0,0,0,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 10 then 'october
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,0,0,1,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 11 then 'november
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,0,1,1,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
elseif month = 12 then 'december
LOOKUP hours, (1,2,2,2,2,2,2,1,0,0,00,00,00,0,0,0,1,1,1,1,1,1,01,01), on_off_hour
' 24 hours 12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11
ENDIF
RETURN
LIGHTON_OFF:
If on_off_hour = 1 then
high b.0
elseif on_off_hour = 0 then
low b.0
endif
If pinb.1 = 1 and on_off_hour = 2 then gosub lit_on_from_sensor
RETURN
LIT_ON_FROM_SENSOR:
High b.0
wait 60
for loopone = 0 to 100
pause 3000
If pinb.1 = 1 then ; if sensor ativates
Ltime = 200
endif
pause 3000
next
for loopone = 0 to Ltime
pause 6000
next
low b.0
RETURN
ALWAYS_ON:
DO
gosub gettime
gosub caltime
If on_off_hour = 0 then
low b.0
elseif on_off_hour => 1 then
high b.0
Endif
pause 5000
LOOP until pinb.2 = 0
RETURN