#picaxe 20m2 'marks
#terminal 38400
SETFREQ M32 ' time increments 0.5 seconds
dirsB = %11111001
dirsC = %10111000
SYMBOL DelayTime = 12 ' so will be 6 seconds
SYMBOL BYPASSswitch = pinB.2
SYMBOL MOTIONtrigger = pinB.1
SYMBOL SSRpower = outpinB.0
SYMBOL Motion_SENSE = b0
SYMBOL secs = b1
SYMBOL mins = B2
SYMBOL hours = B3
SYMBOL date = B4 : SYMBOL day = B4 'day not used from RTC
SYMBOL month = B5
SYMBOL year = B6
SYMBOL CommonYear = b7 : SYMBOL DaySelect = b7 : SYMBOL Motion_ON = b7
SYMBOL D0 = b8
SYMBOL D1 = b9
SYMBOL D2 = b10
SYMBOL D3 = b11
SYMBOL DayNumber = W6
SYMBOL DSTstart = W7
SYMBOL DSTend = w8 : SYMBOL DayS = w8 : SYMBOL SunrisePlus10 = w8
SYMBOL SunRiseTake60 = w9 : SYMBOL Sunrise = w9
SYMBOL SunSet = W10
SYMBOL DayTime = W11
SYMBOL CRC = b24
SYMBOL Auto_ON = b25
TABLE 0,(204,203,202,199,194,189,182,174,166,156) ' Sunrise = Data +240 minutes
TABLE 10,(147,136,126,115,105, 94, 83, 73, 63, 54) ' KillDear , North Dakota
TABLE 20,( 45, 37, 29, 23, 18, 14, 11, 10, 10, 11) ' -7 StandardTime
TABLE 30,( 13, 17, 21, 26, 32, 38, 44, 51, 58, 64)
TABLE 40,( 71, 77, 84, 91, 97,104,111,118,125,133) ' 2018 (betweenleapyear) selected as a better fit as excell rounds up
TABLE 50,(140,148,156,163,171,178,185,191,196,200)
TABLE 60,(202,204)
TABLE 100,( 25, 30, 37, 44, 52, 60, 68, 76, 84, 92) ' Sunset = Data +960 minutes
TABLE 110,(100,107,115,122,129,136,143,150,157,164)
TABLE 120,(171,178,185,191,197,203,207,211,214,215)
TABLE 130,(215,214,212,208,203,197,190,182,173,163)
TABLE 140,(153,143,132,121,110, 99, 89, 78, 68, 58)
TABLE 150,( 49, 41, 34, 27, 22, 18, 16, 15, 15, 17)
TABLE 160,( 21, 25)
InitialiseTime:
HI2Csetup I2Cmaster, %11010000, I2Cslow_32, I2Cbyte ' Set to 100kbps
HI2Cout $0 , ( $30, $59, $22, 0 , $19 , $09 , $17) ' program with UTC/GMT -7 hours StandardTime
TIME = DelayTime
Main:
MotionSwitches: IF MOTIONtrigger = 1 THEN : TIME = 0 : ENDIF 'reset to begining of delaytime
IF BYPASSswitch = 1 THEN : TIME = DelayTime -1 : ENDIF 'holds just before end of delaytime
IF BYPASSswitch = 0 THEN : Motion_SENSE = 0 : ENDIF 'Motion_SENSE =0 (conditional will allow it on until delaytime reached)
conditionals1: IF TIME > DelayTime THEN : TIME = DelayTime : ENDIF 'halts when delaytime reached
IF TIME < DelayTime THEN : Motion_SENSE = 1 : ENDIF 'Motion_SENSE =1 until delaytime reached
HI2Cin $0 , (secs,mins,hours,day,date,month,year) ' Read DS1307 or DS3231
FOR bptr = 2 TO 6
@bptr = @bptr/$10*250+@bptr ' Convert BCD to Decimal
NEXT
DayNumberOfYear:
CommonYear = year //4 +3 /4 ' CommonYear =1
DayNumber = month +9 /12
DayNumber = CommonYear + DayNumber * DayNumber
DayNumber = month *275 /9 +date -30 -DayNumber
StartAndEndDaysForDST: ' United States
DayS = year *512 **46752 ' days = year *365.25
DSTstart = Days +72//7
DSTstart = 74 -CommonYear -DSTstart ' Second SunDay in March
DSTend = DSTstart +238 ' First SunDay in November
SelectSunriseAndSunsetData:
DaySelect = Daynumber /6 : READtable DaySelect,D0 : INC DaySelect : READtable DaySelect,D1 'D0 , D1 holds DataMinutes for SunRise
DaySelect = DaySelect +99 : READtable DaySelect,D2 : INC DaySelect : READtable DaySelect,D3 'D2 , D3 holds DataMinutes for Sunset
DaySelect = DayNumber //6 : IF D0 > D1 THEN : SWAP D0,D1 :DaySelect = 6 -DaySelect : ENDIF
SunRiseTake60 = D1 -D0 * DaySelect /6 +D0 +180
DaySelect = DayNumber //6 : IF D2 > D3 THEN : SWAP D2,D3 : DaySelect = 6 -DaySelect : ENDIF ' example if daynumber is 366 our tabledata for sunset is the last entry at 161
SunSet = D3 -D2 * DaySelect /6 +D2 +960 ' Sunset =25 + 960 (985minutes) /60 gives us our time 16:25
' if our daynumber is between 360 and 366 table data 21 and 25 will be recalculated.
StandardTime: d0 = "S"
IF DayNumber < DSTstart OR DayNumber > DSTend THEN SelectOurDayTime_ONrequirement
IF DayNumber = DSTstart AND hours <2 THEN SelectOurDayTime_ONrequirement ' DST starts at 0200
IF DayNumber = DSTend AND hours >1 THEN SelectOurDayTime_ONrequirement ' DST ends at 0200
AdjustForMountainDaylightTime: d0 = "D" ' DST adjust +1
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 : Sunrise =Sunrise +60 : Sunset =Sunset +60
SelectOurDayTime_ONrequirement:
DayTime = hours *60 +mins ' DayTime = minutes of day
conditionals: ' example 19/09/2017 sunrise 06:35 sunset 18:53
Motion_ON =0 :IF DayTime < SunriseTake60 THEN : Motion_ON =1 : ENDIF ' Motion_ON =1 0000 to 05:35
Auto_ON =0 :IF DayTime => Sunset THEN : Auto_ON =1 : ENDIF : SunRisePlus10 = SunriseTake60 +70 ' Auto_ON =1 18:53 to 23:59
IF DayTime >= SunriseTake60 AND Daytime < SunRisePlus10 THEN : Auto_ON =1 : ENDIF ' Auto_ON =1 05:35 to 06:44
Display:
IF secs <> CRC THEN ' wait for change of secs
Sertxd ("M",d0,"T ")
sertxd ("20",#year,"/",#month,"/",#date) ' Date 20yy/mm/dd
d3=mins/10
d2=mins//10
d1=secs/$10
d0=secs//$10
sertxd (" ",#hours,".",#d3,#d2,".",#d1,#d0) ' 24hr time hh/mm/ss
sunrise = SunriseTake60 +60
d2=sunrise/60
d1=sunrise//60 /10
d0=sunrise//60//10
Sertxd (" Sunrise ",#d2,":",#d1,#d0)
d2=sunset/60
d1=sunset//60 /10
d0=sunset//60//10
Sertxd (" Sunset ",#d2,":",#d1,#d0," TIME ",#TIME," ",#Auto_ON," ",#Motion_ON," ",#Motion_SENSE," ",#SSRpower, cr,lf)
ENDIF : CRC = secs
SSRlatchcontrol: IF Auto_ON = 0 and Motion_ON = 0 THEN : SSRpower = 0 : ENDIF
IF Auto_ON = 0 and Motion_ON = 1 and Motion_SENSE = 0 THEN : SSRpower = 0 : ENDIF
IF Auto_ON = 1 OR Motion_ON = 1 and Motion_SENSE = 1 THEN : SSRpower = 1 : ENDIF
goto main