' PIR Alarm control program ' Aug 2006 ' www.imakeprojects.com ' ' This program interfaces a 12F629 PIC to a PIR unit. ' ' The resulting sensor emits an alarm (and/or triggers a relay) if ' motion is sensed twice within 10 seconds. ' ' This results in a sensor useful for spy-type obstacle courses, or ' movie-style alarms. ' ' When the unit is turned on, an LED blinks fast. The PIR is ignored ' while it warms up. ' ' After warming up (120 seconds), the unit blinks slowly (heartbeat) and ' is active. ' ' While active, if motion is sensed (~1 sec pulse from PIR unit, active LOW), ' the LED lights solid for 10 seconds (ignoring the PIR for the first 2) - ' during which if there is no further movement, the heartbeat resumes. ' ' If there is further movement during those 10 seconds (the first 2 seconds ' ignore the PIR, actually) then the unit goes into ALARM mode. ' ' ALARM mode: Siren on, light on via relay. ' Remains in ALARM mode for 5 seconds. ' ' There is also a /DISABLE switch on pin5 (GP3) ' That disables the unit until RESET. (Needs a pullup) ' ' /RESET is on pin3 (GP4). Reset returns the unit to idle "watching" mode. ' This should also use a pullup. ' ' ' Pin 6 (gp1, aka pin1) = LIGHT out (output) ' Pin 5 (gp2, aka pin2) = /DISABLE in (input) ' Pin 4 (gp3, aka pin3) = PIR in (input) ' Pin 3 (gp4, aka pin4) = /RESET in (input) ' Pin 2 (gp5, aka pin5) = LED out (output) ' Pin 7 (gp0, aka pin8) = AUDIO ALARM out (output) ' symbol CMCON = $19 POKE CMCON, $07 ' Generic val for calibration POKE $3FF, $78 symbol flag = b1 ' Flag value for blinkcontrol output 1 ' LIGHT out input 2 ' /DISABLE input (requires a pullup r to +5V) input 3 ' PIR in input 4 ' /RESET input (requires a pullup resistor to +5V) output 5 ' LED out output 8 ' ALARM sound out low 1 ' Init light OFF low 5 ' Init LED off low 8 ' Init alarm sound OFF flag = 0 warmup: ' Warmup for 120 seconds for PIR unit to settle for b0 = 1 to 120 gosub quickflash ' 1 second quick flashing next b0 start: ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled ' Heartbeat - only do one every 3rd cycle flag = flag + 1 pause 100 if flag <> 20 then firstchance gosub slowflash flag = 0 firstchance: ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled ' While PIR input is LOW, do not start the countdown if pin3 = 0 then start ' Debounce any possible positive PIR output after 25ms pause 25 if pin3 = 0 then start ' ' We have motion sensed from the PIR (pin3 = 1, debounced) ' high 5 ' LED lights solid w2 = 0 waithere: ' Wait, ignoring further triggers unless DISABLE low pause 1 w2 = w2 + 1 ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled if w2 <> 4500 then waithere secondchance: ' Now spend 6.5 seconds (spent 4.5 "dead") in a state of ' one-more-strike-you're-out. So check for another PIR high ' signal once every .5 seconds. If we find one, set off the alarm. for b0 = 1 to 130 if pin3 = 1 then doAlarm ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled pause 50 next b0 goto start ' Begin again if no additional trigger doAlarm: ' Enter alarm state for 5 seconds gosub alarmon w2 = 0 ' Counter for OFF time. waitforcancel: ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled pause 1 w2 = w2 + 1 ' Wait this many ms before turning off despite no cancel if w2 = 5000 then timetoreset if pin4 = 1 then waitforcancel timetoreset: gosub alarmoff goto start end ' Subroutines disabled: gosub alarmoff stilldisabled: if pin4 = 1 then stilldisabled high 5 ' LED on pause 500 low 5 goto start return alarmon: high 1 ' LIGHT on high 5 ' LED on high 8 ' ALARM sound on return alarmoff: low 1 ' LIGHT off low 5 ' LED off low 8 ' ALARM sound off return slowflash: high 5 ' LED on pause 100 low 5 ' LED off pause 100 return quickflash: ' 1 second high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 high 5 ' LED on pause 50 low 5 ' LED off pause 50 return