#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <MFRC522.h>
RTC_DS1307 rtc;
LiquidCrystal lcd(7, 8, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
#define SS_PIN 53 // Slave Select Pin for RFID-RC522 module
#define RST_PIN 12 // Reset Pin for RFID-RC522 module
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
#define TRIG_PIN 10 // Ultrasonic sensor trigger pin
#define ECHO_PIN 11 // Ultrasonic sensor echo pin
bool cardScanned = false;
bool movementDetected = false;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
lcd.print("RTC not found!");
Serial.println("RTC not found!");
while (1);
}
lcd.begin(16, 2);
Serial.println("LCD initialized");
SPI.begin();
mfrc522.PCD_Init();
Serial.println("RFID-RC522 initialized");
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
displayDateTime();
checkRFID(); // Check for RFID card
if (detectMovement()) {
movementDetected = true;
unsigned long movementStart = millis(); // Record the time when movement is detected
while (millis() - movementStart < 20000) { // Display "Welcome" message for 20 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome,");
Serial.println("Movement detected");
if (cardScanned) {
lcd.setCursor(0, 1);
lcd.print("Teddy");
Serial.println("Card scanned. Welcome Teddy.");
delay(10000); // Display "Welcome Teddy" for 10 seconds
cardScanned = false; // Reset cardScanned flag after displaying the message
} else {
lcd.setCursor(0, 1);
lcd.print("please scan");
Serial.println("Please scan RFID card");
}
delay(1000);
}
movementDetected = false; // Reset movementDetected flag after handling the event
}
}
void displayDateTime() {
DateTime now = rtc.now();
lcd.clear();
Serial.println("Displaying date and time");
// Display the time
lcd.setCursor(0, 0);
lcd.print("Time: ");
printTwoDigits(now.hour());
lcd.print(":");
printTwoDigits(now.minute());
lcd.print(":");
printTwoDigits(now.second());
// Display the day of the week and date
String daysOfWeek[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
lcd.setCursor(0, 1);
lcd.print(daysOfWeek[now.dayOfTheWeek()]);
lcd.print(" - ");
printTwoDigits(now.month());
lcd.print("/");
printTwoDigits(now.day());
lcd.print("/");
lcd.print(now.year());
}
void printTwoDigits(int number) {
if (number < 10) {
lcd.print("0");
}
lcd.print(number);
}
bool detectMovement() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
long distance = (duration * 0.034) / 2;
if (distance < 100 && !movementDetected) { // Ensure the message is displayed only once per movement
return true;
} else {
return false;
}
}
void checkRFID() {
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// Select one of the cards
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
// If we reach here, a card was successfully read
Serial.println("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
Serial.println("Card Read Accepted"); // Print message on Serial monitor
lcd.clear(); // Clear the LCD
lcd.setCursor(0, 0);
lcd.print("Card Read"); // Display message on LCD
lcd.setCursor(0, 1);
lcd.print("Accepted");
delay(2000); // Pause to display the message, adjust as needed
cardScanned = true; // Set cardScanned to true upon successful card read
}