منبه بمواعيد الدواء باستخدام الاردوينو

مبتدئ

image_pdf

الكود البرمجي

في البداية عليك تحميل مكتبة وحدة الوقت الحقيقي (RTC).

ارفع كود منبه بمواعيد الدواء باستخدام برنامج اردوينو IDE.

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>

RTC_DS3231 rtc;

const int rs = 12, en = 11, d4 = 6, d5 = 5, d6 = 4, d7 = 3;                 // lcd pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int buzz = 13;
int addr=17;

const int buttonPin2 = 9;
const int buttonPin3 = 8;
const int buttonPin1 = A0;
const int buttonPin4 = 7; // the pin that the pushbutton is attached to
int val2 = 0;
int val3=0;
int pushVal;
int bS1 = 0;         // current state of the button
int lBS1 = 0;     // previous state of the button


int bS2 = 0;         // current state of the button
int lBS2 = 0;

int bS3 = 0;         // current state of the button
int lBS3 = 0;

int bS4 = 0;         // current state of the button
int lBS4 = 0;

// configure the pins to the right mode
int buzz8amHH = 8;          //    HH - hours         ##Set these for reminder time in 24hr Format 
int buzz8amMM = 00;          //    MM - Minute
int buzz8amSS = 00;          //    SS - Seconds

int buzz2pmHH = 8;          //    HH - hours
int buzz2pmMM = 1;          //    MM - Minute
int buzz2pmSS = 00;          //    SS - Seconds

int buzz8pmHH = 8;          //    HH - hours
int buzz8pmMM = 2;          //    MM - Minute
int buzz8pmSS = 00;          //    SS - Seconds



int nowHr, nowMin, nowSec; 

void gwsMessege(){               // print get well soon messege
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Stay Healthy :)");     // Give some cheers
    lcd.setCursor(0, 1);
    lcd.print("Get Well Soon :)");// wish 
}

void helpScreen() {              // function to display 1st screen in LCD
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Press Buttons");
    lcd.setCursor(0, 1);
    lcd.print("for Reminder...!");
 }

void timeScreen() {              // function to display Date and time in LCD screen
  DateTime now = rtc.now();             // take rtc time and print in display
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Time:");
    lcd.setCursor(6, 0);
    lcd.print(nowHr = now.hour(), DEC);
    lcd.print(":");
    lcd.print(nowMin = now.minute(), DEC);
    lcd.print(":");
    lcd.print(nowSec = now.second(), DEC);
    lcd.setCursor(0, 1);
    lcd.print("Date: ");
    lcd.print(now.day(), DEC);
    lcd.print("/");
    lcd.print(now.month(), DEC);
    lcd.print("/");
    lcd.print(now.year(), DEC);
    delay(500);
}

void setup()
{
Wire.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));            // uncomment this to set the current time and then comment in next upload when u set the time
  //rtc.adjust(DateTime(2019, 1, 10, 7, 59, 30));                // manual time set

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Welcome To Our");                                      // print a messege at startup
  lcd.setCursor(0, 1);
  lcd.print("Medicine Reminder");
  delay(1000);
  gwsMessege();
  delay(3000);
  helpScreen();
  delay(2000);
  timeScreen();
  delay(3000);
  lcd.clear();

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buzz, OUTPUT);
  Serial.begin(9600);
}

void ValSet(){
  Serial.println(EEPROM.read(addr));
  val2 = EEPROM.read(addr);                         // read previosuly saved value of push button to start from where it was left previously
  switch (val2) {
    case 1:
      lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Once/day !");
      pushVal = 1;
      delay(500);
      break;
    case 2:
      lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Twice/day !");
      pushVal = 2;
      delay(500);
      break;
    case 3:
      lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Thrice/day !");
      pushVal = 3;
      delay(500);
      break;
  }
}

// the main loop will constantly check to see if the button has been pressed
// if it has, a counter is incremented, and then some action can be taken
void loop() 
{                                         //call to set thrice/day 
    if (pushVal == 1) {                                // if push button 1 pressed then remind at 8am
    at8am();                                           //function to start uzzing at 8am 
  }
  else if (pushVal == 2) {                             // if push button 2 pressed then remind at 8am and 8pm
    at8am();                                            
    at8pm();                                           //function to start uzzing at 8mm
  }
  else if (pushVal == 3) {                             // if push button 3 pressed then remind at 8am and 8pm
    at8am();
    at2pm();                                            //function to start uzzing at 8mm
    at8pm();
  }
  // read the state of the button
  bS1 = digitalRead(buttonPin1);
  bS2 = digitalRead(buttonPin2);
  bS3 = digitalRead(buttonPin3);
  bS4 = digitalRead(buttonPin4);

  // check to see if it different than the last time we checked


if (bS2 != lBS2) {
    // either the button was just pressed or just released
    if (bS2 == HIGH) {
      // it was just pressed
      Serial.println("n02");
      EEPROM.write(17,1);
      push1();
      delay(1000);
    }
  }
  lBS2 = bS2;


if (bS3 != lBS3) {
    // either the button was just pressed or just released
    if (bS3 == HIGH) {
      // it was just pressed
      Serial.println("no3");
      EEPROM.write(17,2);
      push2();
      delay(1000);
    }
  }
  lBS3 = bS3;


if (bS4 != lBS4) {
    // either the button was just pressed or just released
    if (bS4 == HIGH) {
      // it was just pressed
      Serial.println("no4");
      EEPROM.write(17,3);
      push3();
      delay(1000);
    }
  }
  lBS4 = bS4;

if (bS1 != lBS1) {
    // either the button was just pressed or just released
    if (bS1 == HIGH) {
      val3=EEPROM.read(addr);
      EEPROM.write(17,0);
      digitalWrite(buzz, LOW);
      pinstop();
      EEPROM.write(17,val3);
    }
  }
  lBS1 = bS1;

  timeScreen();
  ValSet();
}
void push1() {                   // function to set reminder once/day                                           //save the state of push button-1 
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Once/day !");
    delay(1200);
    lcd.clear();
  }

void push2() {                      //function to set reminder twice/day
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Twice/day !");
    delay(1200);
    lcd.clear();
  }

void push3() {                    //function to set reminder thrice/day
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Reminder set ");
    lcd.setCursor(0, 1);
    lcd.print("for Thrice/day !");
    delay(1200);
    lcd.clear();
  }

void pinstop(){                   //function to stop buzzing when user pushes stop push button
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Take Medicine  ");
    lcd.setCursor(0, 1);
    lcd.print("with Warm Water");
    delay(5000);
    lcd.clear();
  }
void at8am() {                      // function to start buzzing at 8am
  DateTime t = rtc.now();
  if (int(t.hour()) == buzz8amHH && int(t.minute()) == buzz8amMM  && (int(t.second()) == buzz8amSS || int(t.second()) < buzz8amSS+10)) {
        /////////////////////////////////////////////////////
        digitalWrite(buzz,HIGH);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time to take ");
        lcd.setCursor(0, 1);
        lcd.print("Morning medicines.");
        delay(5000);

        /////////////////////////////////////////////////////
      }
}

void at2pm() {                          // function to start buzzing at 2pm
  DateTime t = rtc.now();
  if (int(t.hour()) == buzz2pmHH && int(t.minute()) == buzz2pmMM  && (int(t.second()) == buzz2pmSS || int(t.second()) < buzz2pmSS+10)) {
        /////////////////////////////////////////////////////
        digitalWrite(buzz,HIGH);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time to take ");
        lcd.setCursor(0, 1);
        lcd.print("Afternoon medicines.");
        delay(5000);

        /////////////////////////////////////////////////////
      }
}

void at8pm() {                           // function to start buzzing at 8pm
DateTime t = rtc.now();
  if (int(t.hour()) == buzz8pmHH && int(t.minute()) == buzz8pmMM  && (int(t.second()) == buzz8pmSS || int(t.second()) < buzz8pmSS+10)) {
        /////////////////////////////////////////////////////
        digitalWrite(buzz,HIGH);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Time to take ");
        lcd.setCursor(0, 1);
        lcd.print("Night medicines.");
        delay(5000);

        /////////////////////////////////////////////////////
      }
}

شرح الكود البرمجي

لذلك في البداية نقوم بإدراج مكتبة Wire.h ، التي تحتوى على الدوال اللازمة للتواصل بين الاردوينو والـ RTC Module .كما نقوم أيضًا بإدراج مكتبة LiquidCrystal.h التي تحتوي على الدوال الخاصة بشاشة الـ LCD، والتي سيتم استخدمها لعرض حالة المنبه ومكتبة RTClib.h لتعيين الوقت الحقيقي.

نستطيع تحميل مكتبة الشاشة الكرستالية بتتبع المسار التالي:

Sketch > Include libraries > Manage libraries

ثم نكتب بخانة البحث Liquid crystal by Arduino

ثم نضغط على Install.

#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>

بعد ذلك عرّفنا المتغيرات الخاصة بالشاشة الكرستالية.

const int rs = 12, en = 11, d4 = 6, d5 = 5, d6 = 4, d7 = 3; // lcd pins
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

حددنا المنفذ الذي استخدمناه لربط مصدر الصوت مع الاردوينو=13.

int buzz = 13;

مفاتيح الضغط التي تم ربطها مع الاردوينو بالمنافذ الرقمية 3 وهم buttonPin=9, buttonPin=8, buttonPin4=7.

وهناك مفتاح ضغط واحد تم ربطه بالمنافذ التناظرية وهو buttonPin1=A0.

const int buttonPin2 = 9;
const int buttonPin3 = 8;
const int buttonPin1 = A0;
const int buttonPin4 = 7; // the pin that the pushbutton is attached to

هنا توضيح لحالة مفاتيح الضغط الأربعة الابتدائية =0 جميعها.

int bS1 = 0; // current state of the button
int lBS1 = 0; // previous state of the button

int bS2 = 0; // current state of the button
int lBS2 = 0;

int bS3 = 0; // current state of the button
int lBS3 = 0;

int bS4 = 0; // current state of the button
int lBS4 = 0;

يمكنك تعيين 3 أوقات مختلفة في اليوم لتناول الأدوية أو يمكنك الاكتفاء بوقتين أو وقت واحد.

على سبيل المثال الوقت الأول لتناول الدواء: الساعة الثامنة صباحًا.

int buzz8amHH = 8

int buzz8amMM = 0

int buzz8amSS = 0

الوقت الثاني: الساعة الواحدة و55 دقيقة مساءً.

int buzz2pmHH = 14

int buzz2pmMM = 55

int buzz2pmSS = 0

الوقت الثالث: الساعة السادسة و55 دقيقة و20 ثانية مساءَ.

int buzz8pmHH = 18

int buzz8pmMM = 55

int buzz8pmSS = 20

// configure the pins to the right mode
int buzz8amHH = 8; // HH - hours ##Set these for reminder time in 24hr Format
int buzz8amMM = 0; // MM - Minute
int buzz8amSS = 0; // SS - Seconds

int buzz2pmHH = 14; // HH - hours
int buzz2pmMM = 55; // MM - Minute
int buzz2pmSS = 0; // SS - Seconds

int buzz6pmHH = 18; // HH - hours
int buzz6pmMM = 55; // MM - Minute
int buzz6pmSS = 20; // SS - Seconds

هنا سيتم طباعة في السطر الأول Stay Healthy ابقى بصحة جيدة.

وفي السطر الثاني Get Well Soon ستكون بصحة جيدة قريبًا.

void gwsMessege(){ // print get well soon messege
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stay Healthy :)"); // Give some cheers
lcd.setCursor(0, 1);
lcd.print("Get Well Soon :)");// wish
}

هنا سيتم طباعة في السطر الأول Press Buttons اضغط أيًا من الأزرار.

وفي السطر الثاني for Reminder من أجل المنبه.

void helpScreen() { // function to display 1st screen in LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Buttons");
lcd.setCursor(0, 1);
lcd.print("for Reminder...!");
}

هنا سيتم حساب الوقت والتاريخ باستخدام وحدة الوقت الحقيقي RTC.

void timeScreen() { // function to display Date and time in LCD screen
DateTime now = rtc.now(); // take rtc time and print in display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(6, 0);
lcd.print(nowHr = now.hour(), DEC);
lcd.print(":");
lcd.print(nowMin = now.minute(), DEC);
lcd.print(":");
lcd.print(nowSec = now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
delay(5000);
}

Welcome To Our Medicine Reminder أهلًا بك بنظام المنبه الذكي. هنا سيتم طباعة أول جملة على الشاشة

بعد ذلك عليك الاختيار بين 4 أزارير.

الزر الأول على اليسار: لاختيار منبه يعمل مرة واحدة فقط في اليوم (عليك تعيين الوقت المناسب).

الزر الثاني على اليسار: لاختيار منبه مرتبن في اليوم (عليك تعيين وقتان مناسبان).

الزر الثالث على اليسار: لاختيار منبه ثلاث مرات في اليوم (عليك تعيين ثلاث أوقات مناسبة).

الزر الرابع على اليسار: لإيقاف المنبه.

منبه بمواعيد الدواء

المخرجات ستكون على الشاشة الكرستالية وسيبدأ مصدر الصوت بالعمل لتنبيه المريض بموعد تناول الأدوية.

void setup()
{
Wire.begin();
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // uncomment this to set the current time and then comment in next upload when u set the time
//rtc.adjust(DateTime(2019, 10, 12, 15, 50, 30)); // manual time set

lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome To Our"); // print a messege at startup
lcd.setCursor(0, 1);
lcd.print("Medicine Reminder");
delay(1000);
gwsMessege();
delay(3000);
helpScreen();
delay(2000);
timeScreen();
delay(3000);
lcd.clear();

pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buzz, OUTPUT);
Serial.begin(9600);
}

إذا ضغطت على المفتاح الأول: ستظهر لك رسالة مفادها أنك اخترت المنبه مرة واحدة في اليوم.

إذا ضغطت على المفتاح الثاني: ستظهر لك رسالة مفادها أنك اخترت المنبه مرتين في اليوم.

إذا ضغطت على المفتاح الثالث: ستظهر لك رسالة مفادها أنك اخترت المنبه ثلاث مرات في اليوم.

void ValSet(){
Serial.println(EEPROM.read(addr));
val2 = EEPROM.read(addr); // read previosuly saved value of push button to start from where it was left previously
switch (val2) {
case 1:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Once/day !");
pushVal = 1;
delay(5000);
break;
case 2:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Twice/day !");
pushVal = 2;
delay(5000);
break;
case 3:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Thrice/day !");
pushVal = 3;
delay(5000);
break;
}

هنا سيتم ربط الأزرار مع الشاشة الكرستالية ووحدة الوقت الحقيقي ومصدر الصوت.

إذا تم الضغط على الزر الأول سيعمل المنبه مرة واحدة في اليوم حسب الساعة المعينة بالكود وسينطبع على الشاشة ذلك وسيبدأ مصدر الصوت بالعمل.

وهكذا مع بقية الأزرار.

void loop() 
{ //call to set thrice/day 
if (pushVal == 1) { // if push button 1 pressed then remind at 8am
at8am(); //function to start uzzing at 8am 
}
else if (pushVal == 2) { // if push button 2 pressed then remind at 8am and 8pm
at8am(); 
at8pm(); //function to start uzzing at 8mm
}
else if (pushVal == 3) { // if push button 3 pressed then remind at 8am and 8pm
at8am();
at2pm(); //function to start uzzing at 8mm
at8pm();
}
// read the state of the button
bS1 = digitalRead(buttonPin1);
bS2 = digitalRead(buttonPin2);
bS3 = digitalRead(buttonPin3);
bS4 = digitalRead(buttonPin4);
// check to see if it different than the last time we checked
if (bS2 != lBS2) {
// either the button was just pressed or just released
if (bS2 == HIGH) {
// it was just pressed
Serial.println("n02");
EEPROM.write(17,1);
push1();
delay(1000);
}
}
lBS2 = bS2;
if (bS3 != lBS3) {
// either the button was just pressed or just released
if (bS3 == HIGH) {
// it was just pressed
Serial.println("no3");
EEPROM.write(17,2);
push2();
delay(1000);
}
}
lBS3 = bS3;
if (bS4 != lBS4) {
// either the button was just pressed or just released
if (bS4 == HIGH) {
// it was just pressed
Serial.println("no4");
EEPROM.write(17,3);
push3();
delay(1000);
}
}
lBS4 = bS4;

if (bS1 != lBS1) {
// either the button was just pressed or just released
if (bS1 == HIGH) {
val3=EEPROM.read(addr);
EEPROM.write(17,0);
digitalWrite(buzz, LOW);
pinstop();
EEPROM.write(17,val3);
}
}
lBS1 = bS1;
timeScreen();
ValSet();
}

هنا برمجة الرسالة التي ستنطبع بعد الضغط على الزر الأول Reminder set for Once/day !

void push1() { // function to set reminder once/day //save the state of push button-1 
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Once/day !");
delay(5000);
lcd.clear();
}

هنا برمجة الرسالة التي ستنطبع بعد الضغط على الزر الثاني Reminder set for for Twice/day !

void push2() { //function to set reminder twice/day
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Twice/day !");
delay(5000);
lcd.clear();
}

هنا برمجة الرسالة التي ستنطبع بعد الضغط على الزر الثالث Reminder set forfor Thrice/day !

void push3() { //function to set reminder thrice/day
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Thrice/day !");
delay(5000);
lcd.clear();
}

هنا برمجة الرسالة التي ستنطبع بعد الضغط على الزر الرابع Take Medicinee with Warm Water.

void pinstop(){ //function to stop buzzing when user pushes stop push button
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Take Medicine ");
lcd.setCursor(0, 1);
lcd.print("with Warm Water");
delay(5000);
lcd.clear();
}

هنا برمجة الرسالة التي ستنطبع إذا حان وقت الدواء الأول في الصباح Time to take Morning medicines.

void at8am() { // function to start buzzing at 8am
DateTime t = rtc.now();
if (int(t.hour()) == buzz8amHH && int(t.minute()) == buzz8amMM && (int(t.second()) == buzz8amSS || int(t.second()) < buzz8amSS+10)) {
/////////////////////////////////////////////////////
digitalWrite(buzz,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to take ");
lcd.setCursor(0, 1);
lcd.print("Morning medicines.");
delay(5000);
/////////////////////////////////////////////////////
}
}

هنا برمجة الرسالة التي ستنطبع إذا حان وقت الدواء الثاني في الظهيرة Time to take Afternoon medicines.

void at2pm() { // function to start buzzing at 2pm
DateTime t = rtc.now();
if (int(t.hour()) == buzz2pmHH && int(t.minute()) == buzz2pmMM && (int(t.second()) == buzz2pmSS || int(t.second()) < buzz2pmSS+10)) {
/////////////////////////////////////////////////////
digitalWrite(buzz,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to take ");
lcd.setCursor(0, 1);
lcd.print("Afternoon medicines.");
delay(5000);
/////////////////////////////////////////////////////
}
}

هنا برمجة الرسالة التي ستنطبع إذا حان وقت الدواء الثاني في الظهيرة Time to take Afternoon medicines.

void at8pm() { // function to start buzzing at 8pm
DateTime t = rtc.now();
if (int(t.hour()) == buzz8pmHH && int(t.minute()) == buzz8pmMM && (int(t.second()) == buzz8pmSS || int(t.second()) < buzz8pmSS+10)) {
/////////////////////////////////////////////////////
digitalWrite(buzz,HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time to take ");
lcd.setCursor(0, 1);
lcd.print("Night medicines.");
delay(5000);
/////////////////////////////////////////////////////
}
}

تأكد بأن النظام يعمل بالشكل الصحيح.

لا تنسَ فصل وحدة الطاقة بعد الانتهاء من استخدام النظام.

X
Product added to the cart