Friday, August 21, 2015

PIR alarm with interrupt


int calibrationTime = 10; 


//the time when the sensor outputs a low impulse
long unsigned int lowIn; 

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 6; //the digital pin connected to the PIR sensor's output
int ledPin = 10;
int resetbutton = 2;
volatile boolean buttonState=1;
volatile int buttonState1=1;

/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(resetbutton, INPUT); // Pin 2 is input to which a switch is connected = INT0
attachInterrupt(0, disarm, CHANGE); 
pinMode(pirPin, INPUT); //input pin
pinMode(ledPin, OUTPUT); //alarm pin
digitalWrite(pirPin, LOW);
buttonState = digitalRead(resetbutton);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}

////////////////////////////
//LOOP
void loop(){

if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false; 
Serial.println("---");
//Serial.print("motion detected at ");
//Serial.print(millis()/1000);
//Serial.println(" sec"); 
for(int i=0;i<30;i++){
delay(1000);
if(buttonState1 == 0){ //jika ada interupt
buttonState1 = 1;
digitalWrite(ledPin, LOW);
break; //keluar dari loop for

}
digitalWrite(ledPin, LOW);
}
}
void disarm(){
buttonState1 =0;
}

No comments:

Post a Comment