#include <LiquidCrystal_I2C.h> //library for better communication with
// set the LCD number of columns and rows
int lcdColumns = 16; //allows display of text with 16 characters
int lcdRows = 2; //allows display of text 2 rows
int ledPin = 23; // choose the pin for the LED
int buzzPin = 18; // choose the pin for the buzzer
int inputPin = 19; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detection
int val = 0; // variable for reading the pin status
// set LCD address, number of columns and rows
//if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup() {
pinMode(buzzPin, OUTPUT); // declare buzzer as output
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
// intialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}
void loop() {
val = digitalRead(inputPin); // read input value
if(val == HIGH){ // check if the input is HIGH
digitalWrite(ledPin, LOW); // turn LED off
digitalWrite(buzzPin, HIGH); // turn buzz ON
tone(buzzPin, 100, 6000); //pin number, frequency, duration
//to display the status in the LCD
// set cursor to first column, first row
lcd.setCursor(0,0);
// print message
lcd.print("Motion Detected.");
delay(3000);
// clears the display to print new message
lcd.clear();
// we only want to print on the output change, not state
// set cursor to first column, second row
lcd.setCursor(0, 1);
lcd.print("Area is not safe!");
delay(3000);
lcd.clear();
pirState = HIGH;
}
else{
digitalWrite(ledPin, HIGH); // turn LED on
digitalWrite(buzzPin, LOW); // turn LED off
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("No motion sensed.");
delay(3000);
// clears the display to print new message
lcd.clear();
// We only want to print on the output change, not state
//set cursor to first column, second row
lcd.setCursor(0, 1);
lcd.print("Area is safe!");
delay(3000);
lcd.clear();
pirState = LOW;
}
}