Advertisement
pleasedontcode

**Medication Reminder** rev_01

Apr 14th, 2025
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Medication Reminder**
  13.     - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
  14.     - Source Code created on: 2025-04-14 11:04:03
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Display reminder for pill at 4:40 pm ist via rtc */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  27. #include <Wire.h>
  28. #include <RTClib.h>
  29. #include <LiquidCrystal_I2C.h>
  30.  
  31. // Initialize RTC and LCD
  32. RTC_DS3231 rtc;
  33. LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address may be 0x3F on some LCDs
  34.  
  35. // Medicine info
  36. String medicines[4] = {"Paracetamol", "Vitamin C", "Antibiotic", "Painkiller"};
  37. int reminderHours[4] = {16}; // Reminder set for 4 PM (16:00 in 24hr format)
  38. int reminderMinutes[4] = {40}; // Reminder set for 40 minutes past the hour
  39.  
  40. // Other components
  41. const int ledPin = 14;   // D5 on NodeMCU (Change pin if needed)
  42. const int buzzerPin = 0; // D3 on NodeMCU
  43. const int buttonPin = 2; // D4 on NodeMCU
  44.  
  45. // Flag to track reminders
  46. bool alertTriggered[4] = {false, false, false, false};
  47. unsigned long lastDisplayUpdate = 0;
  48.  
  49. /****** FUNCTION PROTOTYPES *****/
  50. void setup(void);
  51. void loop(void);
  52.  
  53. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  54. const uint8_t Button_PushButton_PIN_D1      = 1; // This pin is not used in the USER CODE
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57.  
  58. void setup(void)
  59. {
  60.     // put your setup code here, to run once:
  61.     Serial.begin(9600);
  62.     Wire.begin();
  63.     rtc.begin();
  64.     lcd.begin(16, 2);  // Corrected begin with cols and rows
  65.     lcd.backlight();
  66.  
  67.     // Setup output pins
  68.     pinMode(ledPin, OUTPUT);
  69.     digitalWrite(ledPin, LOW);
  70.  
  71.     pinMode(buzzerPin, OUTPUT);
  72.     pinMode(buttonPin, INPUT_PULLUP);
  73.  
  74.     lcd.setCursor(0, 0);
  75.     lcd.print("  Pill Time Boot ");
  76.     delay(2000);
  77.     lcd.clear();
  78.  
  79.     pinMode(Button_PushButton_PIN_D1, INPUT_PULLUP); // Keep this for compatibility with EasyButton
  80. }
  81.  
  82. void loop(void)
  83. {
  84.     DateTime now = rtc.now();
  85.  
  86.     // Update time display every second
  87.     if (millis() - lastDisplayUpdate >= 1000) {
  88.         showTime(now);
  89.         lastDisplayUpdate = millis();
  90.     }
  91.  
  92.     // Check reminders
  93.     for (int i = 0; i < 4; i++) {
  94.         if (now.hour() == reminderHours[i] && now.minute() == reminderMinutes[i] && !alertTriggered[i]) {
  95.             triggerReminder(i);
  96.             alertTriggered[i] = true;
  97.         }
  98.  
  99.         // Reset trigger for next day
  100.         if (now.hour() != reminderHours[i] || now.minute() != reminderMinutes[i]) {
  101.             alertTriggered[i] = false;
  102.         }
  103.     }
  104.  
  105.     delay(100);
  106. }
  107.  
  108. void showTime(DateTime now) {
  109.     char timeBuffer[9];
  110.     sprintf(timeBuffer, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
  111.  
  112.     lcd.setCursor(0, 0);
  113.     lcd.print("Time: ");
  114.     lcd.print(timeBuffer);
  115.     lcd.print("  "); // Padding
  116. }
  117.  
  118. void triggerReminder(int index) {
  119.     lcd.clear();
  120.     lcd.setCursor(0, 0);
  121.     lcd.print("Take: ");
  122.     lcd.print(medicines[index].substring(0, 10)); // Fit name in LCD
  123.     digitalWrite(ledPin, HIGH);  // Turn on LED
  124.     tone(buzzerPin, 1000);
  125.  
  126.     // Wait for pushbutton acknowledgment
  127.     while (digitalRead(buttonPin) == HIGH) {
  128.         delay(100);
  129.     }
  130.  
  131.     // Acknowledged
  132.     noTone(buzzerPin);
  133.     digitalWrite(ledPin, LOW);  // Turn off LED
  134.  
  135.     lcd.clear();
  136.     lcd.setCursor(0, 0);
  137.     lcd.print(medicines[index]);
  138.     lcd.setCursor(0, 1);
  139.     lcd.print("Taken ✔");
  140.     delay(2000);
  141.     lcd.clear();
  142. }
  143.  
  144. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement