// source http://www.instructables.com/id/Home-Security-Alarm-or-a-Motion-Detector-Using-Ard/ // Source photo cell https://learn.adafruit.com/photocells/using-a-photocell // Source speaker http://www.instructables.com/id/Home-Security-Alarm-or-a-Motion-Detector-Using-Ard/ // library for the LCD display: #include int ledPin = 11; int PIRpin = 8; int pirState = LOW; int val = 0; // photocell circuit int photocellPin = 0; // the cell and 10K pulldown are connected to a0 int photocellReading; // the analog reading from the sensor divider int pinSpeaker = 10; // must be a pwm pin (9, 10 or 11) LiquidCrystal lcd(2, 3, 4, 5, 6, 7); void setup() { pinMode(ledPin, OUTPUT); pinMode(PIRpin, INPUT); pinMode(photocellPin, INPUT); pinMode(pinSpeaker, OUTPUT); Serial.begin(9600); lcd.begin(16, 2); lcd.setCursor(2, 0); // Set LCD cursor position (column, row) lcd.print("P.I.R Motion "); // Print text to LCD lcd.setCursor(0, 1); // Set LCD cursor position (column,row) lcd.print("and Light Sensors"); // Print text to LCD delay(2000); // wait 2s // Delay to read text lcd.clear(); // Clear LCD lcd.setCursor(0, 0); lcd.print("Processing Data."); playTone(300, 300); delay(150); playTone(0, 0); delay(3000); lcd.clear(); lcd.setCursor(3, 0); lcd.print("Waiting For"); lcd.setCursor(3, 1); lcd.print("Motion...."); delay(150); } void loop(){ val = digitalRead(PIRpin); photocellReading = analogRead(photocellPin); if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); lcd.clear() ; lcd.setCursor(0, 0); // Set LCD cursor position (column 0, row 0) lcd.print("Motion Detected!"); lcd.setCursor(0, 1); // Set LCD cursor position (column 0, row 1) lcd.print(photocellReading); // We only want to print on the output change, not state playTone(300, 300); delay(150); playTone(0, 0); pirState = HIGH; // delay(5000); // when motion display info during 5 seconds } } else { digitalWrite(ledPin, LOW); // turn LED OFF delay(300); // display no motion screen saver scrollScreenSaver() ; if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); pirState = LOW; } } } // duration in mSecs, frequency in hertz void playTone(long duration, int freq) { duration *= 1000; int period = (1.0 / freq) * 100000; long elapsed_time = 0; while (elapsed_time < duration) { digitalWrite(13, HIGH); digitalWrite(pinSpeaker,HIGH); delayMicroseconds(period / 2); digitalWrite(13, LOW); digitalWrite(pinSpeaker, LOW); delayMicroseconds(period / 2); elapsed_time += (period); } } void scrollScreenSaver() { // autoscroll https://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll lcd.clear() ; lcd.setCursor(15, 0); // Set LCD cursor position (column 0, row 0) lcd.print("No Motion "); lcd.setCursor(15, 1); lcd.print("Waiting !"); // scroll 7 positions (display length - string length) to the left // to move it back to center: for (int positionCounter = 0; positionCounter < 22; positionCounter++) { // scroll one position left: lcd.scrollDisplayLeft(); // wait a bit: delay(150); } }