`CODE
' PICAxe 20x2 Code for Sony S600 that has a trail mode.
`With a jumper between b.7 and V you have feeder mode
` spare mosfet to control lens cover

`Edited by Charles Garrett and Floyd Newberry.
'=========
'3/15/11
'=========

'=========================
'Sony S600 4 wire
'=========================




#picaxe20x2
'#terminal 9600
#no_data
#no_table
disablebod

'============================= ========================
' Map Ports
'============================= ========================
Symbol CDS_Out = C.5 ' Output to LDR voltage Divider
Symbol LC_Out = C.1 ' Lens cover switch
Symbol Battery_Monitor = B.6 ' ADC Input for battery monitor
Symbol LDR_IN = B.5 ' LDR ADC Input
Symbol Shutter_Out = B.4 ' Shutter Mosfet
Symbol Power_Out = B.3 ' Power Mosfet
Symbol LED_Out = B.2 ' LED
Symbol PIR_In = B.1 ' Input from PIR Circuit
Symbol BkGrnd_Timer = B1
Symbol Byte_Var = B2
Symbol ValidPIR = B3
Symbol PIR_Event = B4
Symbol WlkTestFlag = B5 ' Walk test status flag
Symbol WlkTestTime = B6 ' Number of 15 sec periods for walk test
Symbol CamPwrState = B7
Symbol Normal_OP = B8
Symbol CamPwrDownTimer = B10
Symbol Event_Delay = B11
Symbol CamEvent_Flag = B12
Symbol Cam_Mode = B13
Symbol PRELOAD = 63890 ' approx 15 sec at 31khz
Symbol CDS_Value = b16
Symbol Night_Flag = b17
Symbol Day = b18
Symbol Night = b19
Symbol TimerLimit = b23
Symbol CamRefresh = W12

'============================= ========================
' Set Port Directions
'============================= ========================
Output CDS_Out
Output LC_Out
Output Battery_Monitor
Input LDR_IN
Output Shutter_Out
Output Power_Out
Output LED_Out
Input PIR_In
Input C.7
Input C.6
Input B.7
Input C.0

`============================= ========================
`Camera settings
`============================= ========================
'Delay after power on
symbol Shutter_pause = 2500

'Amount of time that can pass before the camera needs refreshed
'value=(10*timeinseconds/147)-1
'example if a 73.5s refresh is needed you would set it equal to 4
'4=(10*73.5/147)-1
Symbol CamRefreshLimit = 244

'Timervalue1 and Timervalue2 are in multiples of 5 sec
'Timer value to be used if B.0 is Low
Symbol Timervalue1 = 3

'Timer value to be used if B.0 is High
Symbol Timervalue2 = 6

'============================= ========================
'Pullups
'============================= ========================
'Disable all PUs
pullup %00000000

'============================= ========================
' Configure ADC
'============================= ========================
adcsetup = %010000000000 'make adc 10 a input
'============================= ============================== =================

' STARTUP
'============================= ============================== =================

; Allow board to stabilize for 20 sec
High LED_Out
pause 5000
High LC_out
GoSub CamPWR
pause 5000
GoSub CamPWR
Low LC_Out
pause 20000
Low LED_Out




Powerup:
WlkTestTime = 2 ' set walk test timer to 15sec intervals of timer
WlkTestFlag = 1 ' Turn on walk test mode
CamRefresh = 0
Event_Delay = 2 ' # of 15sec periods to wait before another cam event occurs
CamEvent_Flag = 0 ' Set flag to off
Cam_Mode = 1
Normal_OP = 1 ' board is operating in normal mode if 0. Activity mode or walktest if 1
Night = 155; lux level 7 = 100k(R16) 2v @ 4v 2 X 256 / 4 = 128
Day = 144
Night_Flag = 0
' Do initial board setup for light & battery levels
GoSub CdsChk
'============================= ========================
'Configure Interrupts
hintsetup %01100110 'set HINT2(PIR) & HINT1(FLASH) to trigger Interrupt at rising edge
Setintflags %00000100,%00000100 'enable interrupt flags


'============================= ========================
Main: ' Main loop that checks for a PIR event, charges slave, turns off the cam if in trail mode and increments counters & timers, then sleeps for 15 secs and does it all over again:
'============================= ========================
if Pinb.7 = 0 then 'Set TimerLimit to Number x 5 in seconds
TimerLimit = Timervalue1

else
TimerLimit = Timervalue2
endif

CamPwrDownTimer =0 'resets CamPWRDownTimer so timer can inc properly

If CamPwrState = 1 and pinC.7 = 1 then 'power cam down and wait 1 min if C.7 is jumpered
gosub CamPwr
do while CamPwrDownTimer <= 12 and pinC.7 = 1
pause 5000
inc CamPwrDownTimer
loop
endif

If CamPwrState = 1 then 'if cam is on starts the cam power down timer and checks for PIR event
Do While CamPwrDownTimer <= TimerLimit 'pauses for 5 sec x TimerLimit checking for a PIR event each time
If PIR_Event = 1 Then 'checks Pir for activity. Starts PirIntService and exits timing loop if found
Gosub PIR_IntService
exit
Endif
Pause 5000 'Five sec pause
Inc CamPwrDownTimer
Loop
Inc TimerLimit
If CamPwrDownTimer = TimerLimit then GoSub CamPwr 'turns cam off if timer finished counting
Else
If PIR_Event = 1 Then GoSub PIR_INTService
Endif

' Turn off walk test if time is up
If WlkTestFlag = 1 And WlkTestTime = 0 Then
WlkTestFlag = 0 'turn off walk test If Time is up
Byte_Var = 0 ' Flash LED to indicate walk test over
For Byte_Var = 0 To 7
pause 400
Toggle LED_Out
Next
Pause 3000 ' pause so CdsCHK does not see led and think it's day time
EndIf





' Increment counters & timers
Inc BkGrnd_Timer
If WlkTestFlag = 1 And WlkTestTime <> 0 Then Dec WlkTestTime : EndIf
If WLKTestFlag = 0 Then Inc CamRefresh : EndIf 'Camrefresh timer is passive using the time the processor is in its sleep cycle.

GoSub CdsChk
Sleep 7 'increases the time the processor sleeps by 2.1s:1 on x1 and x2 pics. Ex a value of 2 = 4.2s
If CamRefresh = CamRefreshLimit Then 'set equal to (value+1)x(sleep time). Time will be in seconds. ex (4+1)x14.7s=73.5s refresh
CamRefresh = 0 'time
GoSub CamPwr
Pause 5000
GoSub CamPwr
EndIf
GoTo Main


Interrupt:
'sertxd ("Interrupt ",13,10)
If hint2flag = 1 Then 'Check to see if INT2 has been triggered
PIR_Event = 1 'Set the PIR event flag
hint2flag = 0 'reset hint2flag to 0
EndIf


hintflag = 0

Return


PIR_INTService:

PIR_Event = 0
; +++++++ Now See how long the port is high ++++++++++++++
Byte_Var = 0 'clear the counter
Do While Pinb.1= 1 'While the PIR is high (motion detected)
Inc Byte_Var 'Inc our counter
'Pause 1 'un-comment for a longer period
If Byte_Var >= 2 Then
ValidPIR = 1 'flag the motion detected as valid
Exit
EndIf
Loop



If ValidPIR = 1 Then 'If valid motion then
ValidPIR = 0

' WalkTest
If WlkTestFlag = 1 Then 'if walktest then turn on led to indicate PIR event
WlkTestTime = 2 'reset walktest timer back to original count again
Byte_Var = 0
For Byte_Var = 0 To 19
Toggle LED_Out
Pause 20
Next
Pause 4000; wait a few secs to settle PIR
GoTo PIR_INTService_Exit
EndIf


If WlkTestFlag = 0 Then
If CamPWRState = 0 then GoSub CamPWR ' Turn the camera on for the first time
Pause Shutter_pause
Gosub Shutter
EndIf


EndIf


PIR_INTService_Exit:
'sertxd ("PIR Exit ",13,10)
Setintflags %00000100,%00000100
ValidPIR = 0
Return

LED_Blink:
Byte_Var = 0
For Byte_Var = 0 To 19
Toggle LED_Out
Pause 20

Next
Return


CamPWR:
if campwrstate = 0 then 'stuff to do when cam is turned on
High LC_out
endif
High Power_Out
pause 250
Low Power_Out
CamPwrState = CamPwrState ^ 1 'Toggles CamPwrState Var
if CamPwrState = 0 then 'stuff to do when cam is turned off
Low LC_out
Endif
Return

Shutter:
High Shutter_Out
pause 700
Low Shutter_Out
Return


CdsChk:
High CDS_Out 'Turn on pin to supply voltage to cds voltage divider
readadc 10 , CDS_Value ' Read light value
'sertxd ("CDS Value ",#CDS_Value,13,10)
'IF Night_Flag IS SET THEN WE CHECK TO SEE IF THE LIGHT LEVEL IS > = 144 IF ADC GREATER OR = THEN STILL DARK AND EXIT
'IF ITS BELOW 144 THEN WE CLEAR NITEFLG AND RETURN
'IF Night_Flag = 0 THEN CHECK TO SEE IF ADC ABOVE 155 THEN WE SET NITEFLG AND RETURN
Low CDS_Out 'Turn off voltage divider
If Night_Flag = 1 And CDS_Value >= Day Then
GoTo CdsChk_Exit 'already set and Still dark so we exit
Else
Night_Flag = 0 ' already set and Daylight so clear flag
EndIf
If Night_Flag = 0 And CDS_Value >= Night Then

Else
Night_Flag = 0 ' Daylight so clear flag
EndIf
CdsChk_Exit:

Return