#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
// RTC settings
RTC_DS3231 rtc;
// LCD settings
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
// Buzzer and LED settings
const int buzzerPin = 15; // GPIO pin connected to the buzzer
const int ledPin = 13; // GPIO pin connected to the LED
void setup() {
// Start serial communication
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD screen
// Set buzzer and LED pins as output
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC to compile time
}
// Set initial display message
lcd.setCursor(0, 0);
lcd.print("Medicine Reminder");
lcd.setCursor(0, 1);
lcd.print("System Ready");
delay(2000); // Display message for 2 seconds
lcd.clear();
}
void loop() {
DateTime now = rtc.now();
// Check for medication times and trigger alarms
checkMedicationTime(now, 19, 52, "Aspirin", "Take after meal", "Drink more water"); // 10:15 PM reminder
checkMedicationTime(now, 19, 53, "Paracetamol", "Take with food", "Rest after"); // 10:16 PM reminder
checkMedicationTime(now, 19, 54, "Antibiotic", "Complete full course", "No dairy products"); // 10:17 PM reminder
// Update LCD display every second
lcd.setCursor(0, 0);
lcd.print("Current Time:");
lcd.setCursor(0, 1);
printTime(now);
delay(1000); // Update every second
}
void checkMedicationTime(DateTime now, int hour, int minute, const char* medicineName, const char* instruction1, const char* instruction2) {
if (now.hour() == hour && now.minute() == minute && now.second() == 0) {
// Display medication reminder on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Medicine: ");
lcd.print(medicineName);
lcd.setCursor(0, 1);
lcd.print(instruction1);
lcd.setCursor(0, 2);
lcd.print(instruction2);
// Sound the buzzer and blink the LED for 30 seconds
for (int i = 0; i < 30; i++) {
tone(buzzerPin, 1000); // Sound 1000 Hz tone
digitalWrite(ledPin, HIGH); // Turn LED on
delay(500); // Sound for 500 ms
noTone(buzzerPin); // Stop the tone
digitalWrite(ledPin, LOW); // Turn LED off
delay(500); // Wait for 500 ms
}
lcd.clear();
}
}
void printTime(DateTime time) {
// Print formatted time on LCD
lcd.print(time.hour());
lcd.print(":");
if (time.minute() < 10) {
lcd.print("0");
}
lcd.print(time.minute());
lcd.print(":");
if (time.second() < 10) {
lcd.print("0");
}
lcd.print(time.second());
}