' intruder_alarm.bas ' author: kerwin lumpkins, 2/25/04 ' this program drives an Atmel ATiny26 IC mounted on the Intruder Alarm project ' board to monitor a photoreflective sensor connected to Pin B4. This sensor ' should look at a white block mounted on a door. When the alarm is armed and ' the door is opened, the sensor output voltage will change when the white ' block is not there to reflect its light. The A/D converter on B4 will detect ' this change in voltage past the triggervalue (must be calibrated for each ' sensor) and enter the prealarm state where the user must enter a 3 digit code ' before a timeout expires. If he/she does not sucessfully enter the code (1 ' try only) then the alarm goes off. Declare Sub Flvald Dim Flval As Byte ' This program operates as a state machine. States are Const Disarmed = 0 Const Armed = 1 Const Triggered = 2 Const Alarm = 3 Const Program = 4 ' SW4 or the program switch is on PB6 which is INT0. So while the system is in ' the armed (not triggered) state, the user can enter programming mode and change the code. ' set portb inputs on pb1,2,3 (code switches) b4 (sensor), all others output ' note that by default the program switch on b6 is disabled Ddrb = &B10100000 Ddra = &B11111111 Config Adc = Single , Prescaler = Auto Dim Switches As Byte Dim State As Byte Dim Code_entered(3) As Byte Dim Code_pgmd(3) As Byte Dim Cnt As Byte Dim Hump As Byte Dim Sw1 As Byte , Sw2 As Byte , Sw3 As Byte Dim Sensor As Word Dim Timcnt As Byte Dim Trig As Byte Const Triggervalue = &H333 Led_red Alias Porta.6 Buzzer Alias Porta.4 Led_yel Alias Porta.7 A3 Alias Porta.3 A2 Alias Porta.2 A1 Alias Porta.1 A0 Alias Porta.0 'default code Code_pgmd(1) = 2 Code_pgmd(2) = 1 Code_pgmd(3) = 3 Main: ' state ------ DISARMED ------- State_disarmed: State = Disarmed ' you can end up here after entering code and disarming the unit ' so we want to disable the timer before it fires Disable Interrupts Disable Timer0 ' user feedback to show that unit is in disarmed state Led_red = 0 Led_yel = 0 Wait 1 ' feedback the programmed code If Code_pgmd(1) = 1 Then Flval = 1 If Code_pgmd(1) = 2 Then Flval = 2 If Code_pgmd(1) = 3 Then Flval = 3 Gosub Flvald If Code_pgmd(2) = 1 Then Flval = 1 If Code_pgmd(2) = 2 Then Flval = 2 If Code_pgmd(2) = 3 Then Flval = 3 Gosub Flvald If Code_pgmd(3) = 1 Then Flval = 1 If Code_pgmd(3) = 2 Then Flval = 2 If Code_pgmd(3) = 3 Then Flval = 3 Gosub Flvald ' wait 10 seconds from reset to arm the system Enable Interrupts Enable Int0 On Int0 State_program Wait 6 ' state ----- ARMED ----- State_armed: State = Armed ' turn off interrupt so you can't enter program mode to get around code entry Disable Interrupts Disable Int0 Start Adc Led_yel = 1 Led_red = 0 Wait 1 Trig = 0 While Trig = 0 Sensor = Getadc(7) A3 = Sensor.9 A2 = Sensor.8 A1 = Sensor.7 A0 = Sensor.6 Waitms 10 If Sensor > Triggervalue Then Trig = 1 Wend Goto State_triggered ' state ----- TRIGGERED ----- State_triggered: State = Triggered Led_red = 1 Led_yel = 0 Stop Adc Ddrb = &B10100000 Cnt = 1 'start the countdown timer Config Timer0 = Timer , Prescale = 1024 On Ovf0 Tim0_isr Enable Timer0 Enable Interrupts ' start the 10 second timer and watch for button presses until it fires Nobtn: Waitms 1 Switches = Pinb And &H0E Shift Switches , Right , 1 If Switches = 3 Then Goto Btn3 If Switches = 5 Then Goto Btn2 If Switches = 6 Then Goto Btn1 'any other condition Goto Nobtn Btn1: Led_yel = 1 Code_entered(cnt) = 1 ' wait for button to be released Sw1 = Pinb And &H02 While Sw1 = 0 Sw1 = Pinb And &H02 Wend Waitms 1 Led_yel = 0 Incr Cnt If Cnt = 4 Then Goto Checkcode ' if not, then go get next button press Goto Nobtn Btn2: Led_yel = 1 Code_entered(cnt) = 2 ' wait for button to be released Sw2 = Pinb And &H04 While Sw2 = 0 Sw2 = Pinb And &H04 Wend Waitms 1 Led_yel = 0 Incr Cnt If Cnt = 4 Then Goto Checkcode ' if not, then go get next button press Goto Nobtn Btn3: Led_yel = 1 Code_entered(cnt) = 3 ' wait for button to be released Sw3 = Pinb And &H08 While Sw3 = 0 Sw3 = Pinb And &H08 Wend Waitms 1 Led_yel = 0 Incr Cnt If Cnt = 4 Then Goto Checkcode ' if not, then go get next button press Goto Nobtn ' jump here if 3 button presses detected Checkcode: Led_yel = 0 Wait 1 Check1: If Code_entered(1) = Code_pgmd(1) Then Goto Check2 Else Goto State_alarm 'Led_yel = 1 'Waitms 500 'Led_yel = 0 'Waitms 500 Check2: If Code_entered(2) = Code_pgmd(2) Then Goto Check3 Else Goto State_alarm 'Led_yel = 1 'Waitms 500 'Led_yel = 0 'Waitms 500 Check3: If Code_entered(3) = Code_pgmd(3) Then Goto State_disarmed Else Goto State_alarm ' else the code was correct Goto State_disarmed ' ********************************************* State_program: ' turn off the INT0 interrupt so you can't start this state ' over again by mistake Disable Interrupts Disable Int0 Led_red = 1 Led_yel = 1 Cnt = 1 Nobtnpgm: Waitms 1 Switches = Pinb And &H0E Shift Switches , Right , 1 If Switches = 3 Then Goto Btn3pgm If Switches = 5 Then Goto Btn2pgm If Switches = 6 Then Goto Btn1pgm 'any other condition Goto Nobtnpgm Btn1pgm: Led_yel = 1 Code_pgmd(cnt) = 1 ' wait for button to be released Sw1 = Pinb And &H02 While Sw1 = 0 Sw1 = Pinb And &H02 Wend Waitms 1 Led_yel = 0 Incr Cnt If Cnt = 4 Then Goto Storecode ' if not, then go get next button press Goto Nobtnpgm Btn2pgm: Led_yel = 1 Code_pgmd(cnt) = 2 ' wait for button to be released Sw2 = Pinb And &H04 While Sw2 = 0 Sw2 = Pinb And &H04 Wend Waitms 1 Led_yel = 0 Incr Cnt If Cnt = 4 Then Goto Storecode ' if not, then go get next button press Goto Nobtnpgm Btn3pgm: Led_yel = 1 Code_pgmd(cnt) = 3 ' wait for button to be released Sw3 = Pinb And &H08 While Sw3 = 0 Sw3 = Pinb And &H08 Wend Waitms 1 Led_yel = 0 Incr Cnt If Cnt = 4 Then Goto Storecode ' if not, then go get next button press Goto Nobtnpgm Storecode: Goto State_disarmed ' state ----- ALARM ALARM ALARM ------ State_alarm: Cnt = 1 ' loop until powered off, sound buzzer and flash LED on/off While Cnt = 1 Led_red = 1 Buzzer = 1 Waitms 100 Led_red = 0 Buzzer = 0 Waitms 100 Wend End ' timer 0 Interrupt Service Routine Tim0_isr: Incr Timcnt If Timcnt > 25 Then Goto Timerfired Return Timerfired: Disable Interrupts Goto State_alarm ' ^^^^^^^^^^^ subroutine ^^^^^^^^^^^^^^^ Sub Flvald: For Hump = 1 To Flval Led_yel = 1 Waitms 200 Led_yel = 0 Waitms 200 Next Wait 2 End Sub Flvald