#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD I2C address and dimensions (16 columns and 2 rows)
const int pirPin = 2; // PIR sensor connected to digital pin 2
int motionCount = 0;
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
pinMode(pirPin, INPUT);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
if (digitalRead(pirPin) == HIGH) {
motionCount++;
lcd.setCursor(0, 1);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.print(" Count: ");
lcd.print(motionCount);
delay(500); // Optional delay to avoid multiple counts for a single motion event
}
delay(1000);
// Update the display every 1 second
}