// This program operates the motion sensing camera and alarm circuit. // When the PIR detects motion, the Arduino will tell the camera to take a picture, // and will then set off the buzzer "alarm". The white LED serves as a flash, // while the test LED is only necesary for testing your proto-circuit. // This code is based upon John Park's code at // makeprojects.com/Project/PIR-Sensor-Arduino-Alarm/72/1 // That code, in turn, is based upon the PIR sensor tester by Limor Fried of Adafruit, // and the tone code is based on code by michael@thegrebs.com /////////////////////////////////////// //VARS //int ledPin = 13; // choose the pin for the test LED int pirPin = 12; // choose the input pin (for PIR sensor) int pinSpeaker = 11; //Set up a speaker on a PWM pin (digital 9, 10, or 11) int onPin = 7; // digital pin 6 controls on button (center of camera) int snapPin = 5; // digital pin 5 controls shutter button (top of camera) int ledW = 10; // digital pin 10 controls white LED int tled = 13; // digital pin 13 controls the test LED int calibrationTime = 15; int pirState = LOW; // we start, assuming no motion detected //the time when motion is no longer detected long unsigned int lowstart; //the amount of milliseconds the sensor has to be low //before camera turns off (30 seconds) long unsigned int pause = 30000; boolean offboolean = true; boolean nolowrecord; ////////////////////////////////////// //SETUP void setup() { //pinMode(ledPin, OUTPUT); // declare LED as output pinMode(pirPin, INPUT); // declare sensor as input pinMode(pinSpeaker, OUTPUT); pinMode(ledW, OUTPUT); pinMode(onPin, OUTPUT); pinMode(snapPin, OUTPUT); Serial.begin(9600); //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(){ //This is the HIGH sequence if(digitalRead(pirPin) == HIGH){ //motion is detected by PIR if(offboolean){ //camera is off //turn camera on digitalWrite(onPin, HIGH); // hold down camera on button digitalWrite(tled, HIGH); // test LED turns on with HIGH input delay(250); digitalWrite(onPin, LOW); // release camera on button digitalWrite(tled, LOW); // test LED turns off with LOW input delay(1000); //the on button is also a mode changing button, so we don't want to change the mode //tell arduino that camera is now on, so that it doesn't press the on button again offboolean = false; //record when the motion was detected Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); //take picture //turn on the white LED "flash" digitalWrite(ledW, HIGH); digitalWrite(snapPin, HIGH); // hold down camera shutter button digitalWrite(tled, HIGH); // test LED turns on with HIGH input delay(250); digitalWrite(snapPin, LOW); // release camera shutter button digitalWrite(tled, LOW); // test LED turns off with LOW input //turn off white LED flash digitalWrite(ledW, LOW); delay(500); for(int j = 0; j < 4; j++){ //sound the alarm and flash the white LED tone(pinSpeaker, 160, 300); digitalWrite(ledW, HIGH); // set the white LED on delay(150); tone(pinSpeaker, 120, 300); digitalWrite(ledW, LOW); // set the white LED off delay(150); //reapeats alarm three more times } //this sequence is not a low sequence, so nolowrecord = true; } if(!offboolean){ //camera is on. //this is pretty much the same sequence as if(offboolean), //but arduino leaves on button alone. //record when the motion was detected Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); //take picture //turn on the white LED "flash" digitalWrite(ledW, HIGH); digitalWrite(snapPin, HIGH); // hold down camera shutter button digitalWrite(tled, HIGH); // test LED turns on with HIGH input delay(250); digitalWrite(snapPin, LOW); // release camera shutter button digitalWrite(tled, LOW); // test LED turns off with LOW input //turn off white LED flash digitalWrite(ledW, LOW); delay(500); for(int j = 0; j < 4; j++){ //sound the alarm and flash the white LED tone(pinSpeaker, 160, 300); digitalWrite(ledW, HIGH); // set the white LED on delay(150); tone(pinSpeaker, 120, 300); digitalWrite(ledW, LOW); // set the white LED off delay(150); //reapeats alarm three more times } //this sequence is not a low sequence, so nolowrecord = true; } } //Thus ends the HIGH sequence. //If motion is detected again, HIGH sequence will repeat. //Otherwise, the LOW sequence will begin (below): if(digitalRead(pirPin) == LOW){ //motion no longer detected by PIR if(nolowrecord){ //this is the first time LOW sequence has run since motion was detected //record time that sequence transitioned from HIGH to LOW lowstart = millis(); //make sure arduino does not record this time again //until motion has been detected once more nolowrecord = false; } //If the camera is on, then arduino will read PIR again //to determine if HIGH or LOW sequence is next. //Otherwise . . . if(!offboolean && millis() - lowstart > pause){ //more than 30 sec have passed since transition from HIGH to LOW //because millis() is current time, while lowstart is back when //transition from HIGH to LOW occurred. //camera has turned off offboolean = true; //record when camera turned off Serial.print("camera off at "); Serial.print((millis())/1000); Serial.println(" sec"); delay(50); } } } //The entire loop begins anew.