PIR SENSOR ALARM

PIR alarm

//int rainsense= 0; // analog sensor input pin 0

const int pinSpeaker = 11; // buzzer output

//int countval= 0; // counter value starting from 0

#define NOTE_C4 262

#define NOTE_D4 294

#define NOTE_E4 330

#define NOTE_F4 349

#define NOTE_G4 392

#define NOTE_A4 440

#define NOTE_B4 494

#define NOTE_C5 523

/////////////////////////////

//VARS

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)

int calibrationTime = 30;

//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 = 7; //the digital pin connected to the PIR sensor’s output

int ledPin = 13;

/////////////////////////////

//SETUP

void setup(){

Serial.begin(9600);

pinMode(pirPin, INPUT);

pinMode(ledPin, OUTPUT);

digitalWrite(pirPin, LOW);

pinMode(pinSpeaker, OUTPUT);

//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

alarm();

if(lockLow){

//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”);

delay(50);

}

takeLowTime = true;

}

if(digitalRead(pirPin) == LOW){

digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state

noTone(pinSpeaker);

if(takeLowTime){

lowIn = millis(); //save the time of the transition from high to LOW

takeLowTime = false; //make sure this is only done at the start of a LOW phase

}

//if the sensor is low for more than the given pause,

//we assume that no more motion is going to happen

if(!lockLow && millis() – lowIn > pause){

//makes sure this block of code is only executed again after

//a new motion sequence has been detected

lockLow = true;

Serial.print(“motion ended at “); //output

Serial.print((millis() – pause)/1000);

Serial.println(” sec”);

delay(50);

}

}

}

void alarm(){

tone(pinSpeaker,NOTE_C4, 500);

delay(500);

tone(pinSpeaker,NOTE_D4, 500);

delay(500);

}

 

OTHER SKET

// Uses a PIR sensor to detect movement, buzzes a buzzer

// more info here: http://blog.makezine.com/projects/pir-sensor-arduino-alarm/

// email me, John Park, at jp@jpixl.net

// based upon:

// PIR sensor tester by Limor Fried of Adafruit

// tone code by michael@thegrebs.com

int ledPin = 13; // choose the pin for the LED

int inputPin = 7; // choose the input pin (for PIR sensor)

int pirState = LOW; // we start, assuming no motion detected

int val = 0; // variable for reading the pin status

int pinSpeaker = 11; //Set up a speaker on a PWM pin (digital 9, 10, or 11)

void setup() {

pinMode(ledPin, OUTPUT); // declare LED as output

pinMode(inputPin, INPUT); // declare sensor as input

pinMode(pinSpeaker, OUTPUT);

Serial.begin(9600);

}

void loop(){

val = digitalRead(inputPin); // read input value

if (val == HIGH) { // check if the input is HIGH

digitalWrite(ledPin, HIGH); // turn LED ON

playTone(300, 160);

delay(150);

if (pirState == LOW) {

// we have just turned on

Serial.println(“Motion detected!”);

// We only want to print on the output change, not state

pirState = HIGH;

}

} else {

digitalWrite(ledPin, LOW); // turn LED OFF

playTone(0, 0);

delay(300);

if (pirState == HIGH){

// we have just turned of

Serial.println(“Motion ended!”);

// We only want to print on the output change, not state

pirState = LOW;

}

}

}

// duration in mSecs, frequency in hertz

void playTone(long duration, int freq) {

duration *= 1000;

int period = (1.0 / freq) * 1000000;

long elapsed_time = 0;

while (elapsed_time < duration) {

digitalWrite(pinSpeaker,HIGH);

delayMicroseconds(period / 2);

digitalWrite(pinSpeaker, LOW);

delayMicroseconds(period / 2);

elapsed_time += (period);

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s