#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int ledPin = 7;
const int buzzerPin = 8;
const int buttonPin = 2;
// Reminder time: 10:05 AM
const int reminderHour = 11;
const int reminderMinute = 25;
bool reminderActive = false;
bool medicineTakenToday = false;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Internal pull-up resistor
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
if (!rtc.begin()) {
Serial.println("RTC not found!");
while (1);
}
Serial.println("Smart Pill Reminder Started");
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.hour()); Serial.print(":");
Serial.print(now.minute()); Serial.print(":");
Serial.println(now.second());
// Trigger reminder at set time
if (now.hour() == reminderHour && now.minute() == reminderMinute
&& !medicineTakenToday) {
reminderActive = true;
}
// Alert state
if (reminderActive) {
digitalWrite(ledPin, HIGH);
tone(buzzerPin, 1000);
if (digitalRead(buttonPin) == LOW) { // Button pressed
Serial.println("Medicine Taken!");
digitalWrite(ledPin, LOW);
noTone(buzzerPin);
reminderActive = false;
medicineTakenToday = true;
while (digitalRead(buttonPin) == LOW) { delay(10); }
}
}
// Reset at midnight for next day
if (now.hour() == 0 && now.minute() == 0 && now.second() == 0) {
medicineTakenToday = false;
Serial.println("Daily reminder reset.");
}
delay(1000);
}