#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <PIR.h>
#define timeSeconds 10
#define I2C_ADDR 0x27
#define TOTAL_ROWS 2
#define TOTAL_COLUMNS 16
const int motionSensor = 27;
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
boolean motion = false;
LiquidCrystal_I2C lcd(I2C_ADDR, TOTAL_COLUMNS, TOTAL_ROWS);
void IRAM_ATTR detectsMovement() {
startTimer = true;
lastTrigger = millis();
}
void setup() {
Serial.begin(115200);
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
}
void loop() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HELLO BHAVITHA");
delay(2000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Lets Count 0-20!");
delay(2000);
lcd.clear();
for (int i = 0; i <= 20; i++) {
lcd.setCursor(0, 1);
lcd.print(i);
delay(1000);
lcd.clear();
}
// Current time
now = millis();
if(motion == false) {
Serial.println("MOTION DETECTED!!!");
motion = true;
}
// Turn off the LED after the number of seconds defined in the timeSeconds variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
startTimer = false;
motion = false;
}
}