'CODE
' PICAxe 20x2 Code for Panasonic that has a trail mode.
`With a jumper between C.7 and V you have feeder mode
`Edited by Charles Garrett and Floyd Newberry.
'=========
'3/15/11
'=========
'=========================
'Panasonic 3 wire
'=========================
#picaxe20x2
'#terminal 9600
#no_data
#no_table
disablebod
'=====================================================
' Map Ports
'=====================================================
'Symbol PT_IN = B.0 ' Photo Transistor input
'Symbol Spare_Out = C.1 ' Spare Mosfet
Symbol PT_Out = C.2 ' Photo Transistor output
Symbol Flash_Charge = C.3 ' Flash Charge Mosfet
Symbol Flash_Out = C.4 ' slave flash opto
Symbol CDS_Out = C.5 ' Output to LDR voltage Divider
'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 Byte_Var = B1
Symbol ValidPIR = B2
Symbol PIR_Event = B3
Symbol WlkTestFlag = B4 ' Walk test status flag
Symbol WlkTestTime = B5 ' Number of 15 sec periods for walk test
Symbol CamPwrState = B6
Symbol CamPwrDownTimer = B7
Symbol Flash_Flag = B8' Flag to count the # of flashes seen
Symbol CDS_Value = B9
Symbol Night_Flag = B10
Symbol Day = B11
Symbol First_Flash = b15
Symbol Night = B12
Symbol SlaveRefresh = B13
Symbol Flashcharge = b21
Symbol TimerLimit = B14
Symbol IsFlashCharged = B15
Symbol Power_On_Delay = W8
Symbol Slave_Charge_Time = W9
Symbol CamRefresh = W10
Symbol To_Counter = W11
Symbol PRELOAD = 63890 ' approx 15 sec at 31khz
'=====================================================
' Set Port Directions
'=====================================================
'Input PT_IN
'Output Spare_Out
Output PT_Out
Output Flash_Charge
Output Flash_Out
Output CDS_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 Setup
'=====================================================
'Time to hold in the shutter button on camera in ms
Symbol Shutter_Time = 250
'Time Required to charge up slave in ms
'Set equal to 1 if a Slave Flash isn't used
Slave_Charge_Time = 10000
'time to hold in power button on camera in ms.
'Set equal to 1 if camera uses a switch instead of a momentary push button
Power_On_Delay = 250
'Delay after power on
symbol Shutter_pause = 1200
'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 an input
'============================================================================
' STARTUP
'============================================================================
; Allow board to stabilize for 20 sec
High LED_Out
pause 5000
GoSub CamPWR
pause 5000
GoSub CamPWR
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
Night = 155; lux level 7 = 100k(R16) 2v @ 4v 2 X 256 / 4 = 128
Day = 144
Night_Flag = 0
SlaveRefresh = 0
CamPwrState = 0
IsFlashCharged = 0
' Do initial board setup for light & battery levels
GoSub CdsChk
IF Night_Flag = 1 And Slave_Charge_Time <> 1 Then GoSub Slave_Charge
'=====================================================
'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 (12x5s) if C.7 is jumpered
gosub CamPwr 'timer can be changed to use the timer limit settings for
do while CamPwrDownTimer <= 12 and pinC.7 = 1 'the other mode by changing the do while loop condition
'to TimerLimit instead of a number
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
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
' Charge slave every 10 minutes if night_flag = 1 (40 15 second periods)
If SlaveRefresh = 40 And Night_Flag = 1 And Slave_Charge_Time <> 1 Then GoSub Slave_Charge
'reset slave refresh timer and mark slave flash as being uncharged
If SlaveRefresh = 40 And Night_Flag = 0 Then
SlaveRefresh = 0
IsFlashCharged = 0
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
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.
Inc SlaveRefresh
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
'It is important to note that changing the sleep time will affect all passive timers
'used in the program. Use caution
If CamRefresh = CamRefreshLimit Then
CamRefresh = 0
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
If hint1flag = 1 Then ' Flash has been detected
'sertxd ("Flash Flags ", #Flash_Flag,13,10)
Inc Flash_Flag
hint1flag = 0
Setintflags %00000010,%00000010 'enable interrupt flags FOR FLASH ONLY!
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 Night_Flag = 1 And Slave_Charge_Time <> 1 Then
High PT_Out ' Turn on PT to detect flash
Setintflags %00000010,%00000010 'enable interrupt flags FOR FLASH ONLY!
If IsFlashCharged = 0 Then GoSub Slave_Charge 'if flash isn't charged recharge it
EndIf
Flash_Flag = 0
If WlkTestFlag = 0 Then
If CamPWRState = 0 then GoSub CamPWR ' Turn the camera on for the first time
Pause Shutter_pause
If night_flag =1 And Slave_Charge_Time <> 1 then
GoSub Fire_Flash
Else
Gosub Shutter
EndIf
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:
CamPwrState = CamPwrState ^ 1 'Toggles CamPwrState Var
if campwrstate = 1 then 'stuff to do when cam is turned on
High Power_Out
Endif
if CamPwrState = 0 then 'stuff to do when cam is turned off
Low Power_Out
Endif
Return
Shutter:
High Shutter_Out
pause Shutter_Time
Low Shutter_Out
Return
Fire_Flash:
SetFreq m32
flash_flag = 0
'sertxd ("Flash Start ",13,10)
'Gosub Shutter
High Shutter_Out
Do
Inc TO_Counter
If TO_Counter =10000 Then
Flash_Flag = 0 ' Reset flags
TO_Counter = 0
GoSub Flash_Led ' Flash Led to indicate time out.
GoTo Flash_Exit
EndIf
Loop while flash_flag <> 2
' FireFlash
'If Flash_Flag = 2 Then
High Flash_Out
pause 400
Low Flash_Out
FlashCharge = 1 ' Flash fired so set flag to charge it.Flash_Flag = 0 ' Reset flags
TO_Counter = 0
GoTo Flash_Exit
'EndIf
Flash_Exit:
Low Shutter_Out
First_Flash = 0
Flash_Flag = 0
Low PT_Out
SetFreq m8
'sertxd ("Flash Exit ",13,10)
Return
Flash_Led:
For b0 = 1 To 50
Toggle LED_Out
pause 20
Next
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 NIGHT_FLG AND RETURN
'IF Night_Flag = 0 THEN CHECK TO SEE IF ADC ABOVE 155 THEN WE SET NIGHT_FLG 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
Night_Flag = 1 ' Its dark so set flag
If IsFlashCharged = 0 And Slave_Charge_Time <> 1 Then GoSub Slave_Charge ' Charge the slave up also if it needs it.
Else
Night_Flag = 0 ' Daylight so clear flag
EndIf
CdsChk_Exit:
Return
Slave_Charge:
High Flash_Charge
Pause Slave_Charge_Time ' pause to allow cap to charge up
Low Flash_Charge
SlaveRefresh = 0 ' restart our slave refresh timer.
IsFlashCharged = 1
Return