Medicine reminder and Alart system using Arduino and gsm



working process:

The given Arduino program is a medicine reminder system that helps users take their medication on time. When the system starts, it initializes the RTC module, LCD display, GSM module, and buttons. It then checks if the RTC has lost power and sets the current time accordingly. The user can set up to three alarm times by pressing the SET button and using the INC and NEXT buttons to adjust the hours and minutes for each alarm. Once alarms are set, the system continuously displays the current time and date on the LCD. It also checks every second if the current time matches any of the set alarm times. When a match is found, the system alerts the user by displaying a reminder message, turning on a specific LED (red, yellow, or green), and activating a buzzer. The user has five seconds to press the CANCEL button to acknowledge the alert. If the alert is not canceled within that time, the system sends an SMS notification to a predefined phone number using the GSM module, informing that the medicine was not taken. This ensures that the user or caretaker is notified promptly.



#include <Wire.h>

#include <RTClib.h>

#include <LiquidCrystal.h>

#include <SoftwareSerial.h>


LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

RTC_DS3231 rtc;

SoftwareSerial GSM(3, 2);  // TX, RX for GSM


char phone_no[] = "+88015********";


// Button pins

const int INC = 5;

const int NEXT = 6;

const int SET_BTN = 4;

const int CANCEL_BTN = A3;


// Output pins

const int RED = A0;

const int YELLOW = A1;

const int GREEN = A2;

const int BUZZER = 13;


struct AlarmTime {

  uint8_t hour;

  uint8_t minute;

};


AlarmTime alarms[3]; // 3 alarms

uint8_t lastCheckedMinute = 61;


// ====================== Setup ===========================

void setup() {

  Serial.begin(115200);

  GSM.begin(9600);

  Wire.begin();

  rtc.begin();

  lcd.begin(16, 2);


  // Pin modes

  pinMode(INC, INPUT_PULLUP);

  pinMode(NEXT, INPUT_PULLUP);

  pinMode(SET_BTN, INPUT_PULLUP);

  pinMode(CANCEL_BTN, INPUT_PULLUP);

  pinMode(RED, OUTPUT);

  pinMode(YELLOW, OUTPUT);

  pinMode(GREEN, OUTPUT);

  pinMode(BUZZER, OUTPUT);


  // Splash screen

  lcd.setCursor(0, 0);

  lcd.print("Medicin Reminder");

  lcd.setCursor(0, 1);

  lcd.print("    -SYSTEM- ");

  delay(2000);

  lcd.clear();


  if (rtc.lostPower()) {

    rtc.adjust(DateTime(__DATE__, __TIME__));

  }


  Serial.println("Initializing GSM...");

  initModule("AT", "OK", 1000);

  initModule("ATE1", "OK", 1000);

  initModule("AT+CPIN?", "READY", 1000);

  initModule("AT+CMGF=1", "OK", 1000);

  initModule("AT+CNMI=2,2,0,0,0", "OK", 1000);

  Serial.println("GSM Initialized");

}


// ====================== Loop ============================

void loop() {

  DateTime now = rtc.now();


  if (isButtonPressed(SET_BTN)) {

    setAlarms();

  }


  displayTime(now);

  checkAlarms(now);


  delay(500);  // Keeps LCD updated every 0.5s

}


// ================== Display Time ========================

void displayTime(DateTime now) {

  lcd.setCursor(0, 0);

  lcd.print("Time: ");

  printDigits(now.hour());

  lcd.print(":");

  printDigits(now.minute());

  lcd.print(":");

  printDigits(now.second());


  lcd.setCursor(0, 1);

  lcd.print("Date: ");

  printDigits(now.day());

  lcd.print("-");

  printDigits(now.month());

  lcd.print("-");

  lcd.print(now.year());

}


// ================ Print with Leading 0 ==================

void printDigits(int digits) {

  if (digits < 10) lcd.print('0');

  lcd.print(digits);

}


// ================ Button Debounce ======================

bool isButtonPressed(int pin) {

  if (digitalRead(pin) == LOW) {

    delay(200);

    while (digitalRead(pin) == LOW); // wait for release

    return true;

  }

  return false;

}


// ================== Set Alarms =========================

void setAlarms() {

  for (int i = 0; i < 3; i++) {

    int hour = 0, minute = 0;

    bool settingHour = true;

    lcd.clear();

    lcd.setCursor(0, 0);

    lcd.print("SetAlarm ");


        if (i + 1 == 1) {

      lcd.setCursor(9, 0);

      lcd.print("Morning");

    }

    if (i + 1 == 2) {

      lcd.setCursor(9, 0);

      lcd.print("Noon");

    }

    if (i + 1 == 3) {

      lcd.setCursor(9, 0);

      lcd.print("Night");

    }




    //lcd.print(i + 1);


    while (true) {

      lcd.setCursor(0, 1);

      printDigits(hour);

      lcd.print(":");

      printDigits(minute);

      lcd.print("   ");


      if (isButtonPressed(INC)) {

        if (settingHour) hour = (hour + 1) % 24;

        else minute = (minute + 1) % 60;

      }


      if (isButtonPressed(NEXT)) {

        if (settingHour) settingHour = false;

        else break;

      }

    }


    alarms[i].hour = hour;

    alarms[i].minute = minute;

  }


  lcd.clear();

  lcd.print("Alarms Set!");

  delay(1500);

  lcd.clear();

}


// ================== Alarm Checker ======================

void checkAlarms(DateTime now) {

  if (now.minute() == lastCheckedMinute) return;

  lastCheckedMinute = now.minute();


  for (int i = 0; i < 3; i++) {

    if (now.hour() == alarms[i].hour && now.minute() == alarms[i].minute) {

      triggerAlarm(i);

    }

  }

}


// ================= Trigger Alarm =======================

void triggerAlarm(int i) {

  lcd.clear();

  lcd.setCursor(0, 0);

  lcd.print("Take Medicine!");

  lcd.setCursor(0, 1);

  lcd.print("Group: ");


        if (i + 1 == 1) {

        lcd.setCursor(7, 1);

        lcd.print("Morning");

      }

      if (i + 1 == 2) {

        lcd.setCursor(7, 1);

        lcd.print("Noon");

      }

      if (i + 1 == 3) {

        lcd.setCursor(7, 1);

        lcd.print("Night");

      }


      

  

  //lcd.print(i + 1);


  digitalWrite(RED, i == 0);

  digitalWrite(YELLOW, i == 1);

  digitalWrite(GREEN, i == 2);

  tone(BUZZER, 1000);

  delay(2000);


  unsigned long start = millis();

  bool canceled = false;


  while (millis() - start < 5000) {

    if (digitalRead(CANCEL_BTN) == LOW) {

      canceled = true;

      break;

    }

  }


  noTone(BUZZER);

  digitalWrite(RED, LOW);

  digitalWrite(YELLOW, LOW);

  digitalWrite(GREEN, LOW);

  lcd.clear();


  if (!canceled) {

    sendSMS(phone_no, "ALERT: Patient didn't take medicine.");

  }


  delay(1000); // Prevent multiple triggers

}


// ================== Send SMS ===========================

void sendSMS(char *number, char *msg) {

  GSM.print("AT+CMGS=\"");

  GSM.print(number);

  GSM.println("\"\r");

  delay(500);

  GSM.println(msg);

  delay(500);

  GSM.write(26); // Ctrl+Z to send

  delay(3000);

}


// ================== GSM Init ===========================

void initModule(String cmd, char *res, int t) {

  while (1) {

    GSM.println(cmd);

    delay(100);

    if (GSM.find(res)) {

      delay(t);

      return;

    } else {

      Serial.println("Retrying: " + cmd);

    }

    delay(t);

  }

}

মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

Construction and Testing of a Step-Down DC Chopper Circuit

Construction and Testing of a Step-Up DC Chopper Circuit