#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
RTC_DS1307 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int potPin = A0; // connect potentiometer to A0 pin
int potValue = 0;
struct Medicine {
const char *name;
int dose;
int hour;
int minute;
};
Medicine medicines[] = {
{"Medicine 1", 1, 9, 0},
{"Medicine 2", 2, 12, 0},
{"Medicine 3", 1, 15, 0}
};
int numMedicines = sizeof(medicines) / sizeof(medicines[0]);
const int buzzerPin = 8;
const int buttonPin = 7;
const int ledPin = 9;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
lcd.begin(16, 2);
lcd.print("Medicine Alarm");
delay(2000);
Wire.begin();
rtc.begin();
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(potPin, INPUT);
}
void loop() {
potValue = analogRead(potPin);
int minutesToAdd = map(potValue, 0, 1023, 0, 60);
DateTime now = rtc.now() + TimeSpan(0, minutesToAdd, 0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(" ");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(0, 1);
for (int i = 0; i < numMedicines; i++) {
lcd.print(medicines[i].name);
lcd.print(": ");
lcd.print(medicines[i].dose);
lcd.print("x ");
lcd.print(medicines[i].hour, DEC);
lcd.print(":");
if (medicines[i].minute < 10) {
lcd.print("0");
}
lcd.print(medicines[i].minute, DEC);
lcd.print(" ");
if (now.hour() == medicines[i].hour && now.minute() == medicines[i].minute) {
digitalWrite(ledPin, HIGH);
while (digitalRead(buttonPin) == LOW) {
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
}
digitalWrite(ledPin, LOW);
}
}
delay(1000);
}