' Laser Tripwire control program ' Sep 2006 ' imakeprojects.com ' ' This program interfaces a 12F629 PIC to a photocell and laser for ' purposes of making a Hollywood-style laser tripwire. ' ' The resulting sensor emits an alarm (and/or triggers a relay) if ' the laser is broken for longer than a couple 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 while the CdS cell calibrates. ' ' Afterwards, the unit blinks slowly (heartbeat) and ' is active. ' ' While active, if the laser beam is broken, the unit chirps the alarm 3 ' times quickly and lights the LED solid. If the beam is still broken ' after 3 chirps,the alarm is triggered for 10 seconds. ' After which the unit resumes its normal functions if the beam is unbroken ' after those 10 seconds. ' ' There is also a /DISABLE switch on pin5 (GP3) ' That disables the unit until RESET. (Needs a pullup) ' ' Pin 6 (gp1, aka pin1) = PHOTOCELL IN (input) ' Pin 5 (gp2, aka pin2) = /DISABLE in (input) ' Pin 4 (gp3, aka pin3) = unused ' 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 symbol newcds = b2 symbol oldcds = b3 symbol cdsdiff = b4 symbol threshold = b5 ' This will be auto-calibrated symbol alertflag = b6 symbol scale = 255 ' Scale value for POT command to read CdS cell input 1 ' PHOTOCELL IN input 2 ' /DISABLE input (requires a pullup to +5V) input 3 ' /RESET input (requires pullup to +5v) input 4 ' unused 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: ' Calibrate the sensor - assuming LASER is illuminating the ' PHOTOCELL. ' This reading is the LIT BY LASER reading pot 1, scale, oldcds ' Now read until the beam is broken finalscan: gosub quickflash ' 2 Readings just to let the CdS cell settle pot 1, scale, newcds pause 100 if newcds <= oldcds then finalscan pot 1, scale, newcds if newcds <= oldcds then finalscan threshold = newcds - oldcds threshold = threshold / 3 threshold = threshold * 2 ' Our threshold is now in 'threshold', should be 66% of what ' was finally read. waitforbeam: high 5 ' LED on pot 1, scale, flag if flag >= newcds then waitforbeam low 5 ' LED off pause 200 start: alertflag = 0 ' We are always not in alerted mode if we get here pot 1,scale,oldcds ' Get an initial reading ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled ' Heartbeat - only do one every 50th cycle flag = flag + 1 pause 50 if flag <> 50 then firstchance gosub slowflash flag = 0 firstchance: ' Check if /DISABLE has been pulled low if pin2 = 0 then disabled ' While PHOTOCELL is illuminated by the laser (ie while ' the POT reading hasn't gone UP by a lot) then do not ' start the countdown testing: ' We populated 'oldcds' at beginning of START pause 100 pot 1,scale,newcds if newcds > oldcds then newhigher 'if oldcds > newcds then oldhigher goto start newhigher: cdsdiff = newcds - oldcds if cdsdiff >= threshold then alert goto start 'oldhigher: 'cdsdiff = oldcds - newcds 'if cdsdiff >= threshold then alert goto start alert: ' Possible alarm. ' If we were already in alert, do the alarm instead. ' otherwise give the warning chirp if alertflag = 1 then doAlarm gosub chirp pause 300 gosub chirp pause 300 gosub chirp pause 300 ' If beam is still broken, then alarm. alertflag = 1 goto testing doAlarm: ' Enter alarm state for 10 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 = 10000 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 chirp: high 8 ' Alarm sound on pause 25 low 8 ' Alarm sound off return alarmon: high 5 ' LED on high 8 ' ALARM sound on return alarmoff: 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