#include <LiquidCrystal.h>
#include <RTClib.h>
#include <Wire.h>
/*
* Smart Medicine Reminder
* This Arduino sketch controls an LCD display, RTC, push buttons, and a buzzer
* to remind users to take their medicine at scheduled times. The user can set
* the time, number of dosages, and the next medicine time using the push buttons.
*/
// LCD pins
const int rs = 12, en = 11, d4 = 6, d5 = 5, d6 = 4, d7 = 3;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// RTC
RTC_DS3231 rtc;
// Buzzer pin
const int buzzerPin = 13;
// Push buttons
const int button1Pin = A0;
const int button2Pin = 7;
const int button3Pin = 8;
const int button4Pin = 9;
// Variables to store settings
int setHour = 0;
int setMinute = 0;
int setDosages = 0;
DateTime nextMedicineTime;
void setup() {
// Initialize LCD
lcd.begin(16, 2);
lcd.print("Medicine Reminder");
delay(1000);
lcd.clear();
// Initialize RTC
if (!rtc.begin()) {
lcd.print("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
lcd.print("RTC lost power");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize buzzer
pinMode(buzzerPin, OUTPUT);
// Initialize push buttons
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(button4Pin, INPUT_PULLUP);
}
void loop() {
DateTime now = rtc.now();
// Display current time
lcd.setCursor(0, 0);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
// Check push buttons for setting time and dosage
if (digitalRead(button1Pin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(button1Pin) == LOW) {
// Code to set hour
lcd.setCursor(0, 1);
lcd.print("Set Hour: ");
setHour = (setHour + 1) % 24; // Increment hour
lcd.print(setHour);
delay(1000); // Wait for 1 second to avoid multiple increments
}
}
if (digitalRead(button2Pin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(button2Pin) == LOW) {
// Code to set minute
lcd.setCursor(0, 1);
lcd.print("Set Minute: ");
setMinute = (setMinute + 1) % 60; // Increment minute
lcd.print(setMinute);
delay(1000); // Wait for 1 second to avoid multiple increments
}
}
if (digitalRead(button3Pin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(button3Pin) == LOW) {
// Code to set number of dosages
lcd.setCursor(0, 1);
lcd.print("Set Dosages: ");
setDosages = (setDosages + 1) % 10; // Increment dosages
lcd.print(setDosages);
delay(1000); // Wait for 1 second to avoid multiple increments
}
}
if (digitalRead(button4Pin) == LOW) {
delay(50); // Debounce delay
if (digitalRead(button4Pin) == LOW) {
// Code to confirm settings
lcd.setCursor(0, 1);
lcd.print("Confirm");
nextMedicineTime = DateTime(now.year(), now.month(), now.day(), setHour, setMinute, 0);
delay(1000); // Wait for 1 second to avoid multiple confirmations
}
}
// Check if it's time for medicine
if (now >= nextMedicineTime) {
digitalWrite(buzzerPin, HIGH);
delay(1000);
digitalWrite(buzzerPin, LOW);
// Set next medicine time based on dosages
nextMedicineTime = nextMedicineTime + TimeSpan(0, 24 / setDosages, 0, 0);
}
delay(1000); // Update every second
}