🔒 Closed Arduino #4

Status
Not open for further replies.

Kulantoy

Forum Veteran
swing servo motor back and forth when detected a sound by sound sensor or a rain sensor has detected and stop when thiers no detected
 
wiring please
Here is the wiring diagram for the project:

medicine reminder wiring diagram


Here is a description of the wiring connections:

  • LCD RS pin to Arduino digital pin 7
  • LCD EN pin to Arduino digital pin 8
  • LCD D4 pin to Arduino digital pin 9
  • LCD D5 pin to Arduino digital pin 10
  • LCD D6 pin to Arduino digital pin 11
  • LCD D7 pin to Arduino digital pin 12
  • Red LED pin to Arduino digital pin 2
  • Yellow LED pin to Arduino digital pin 3
  • Green LED pin to Arduino digital pin 4
  • Buzzer pin to Arduino digital pin 5
  • Button pin to Arduino digital pin 6

Make sure to connect the positive and negative terminals of the LEDs and buzzer to the appropriate pins on the Arduino as well.
 
Here is the wiring diagram for the project:

medicine reminder wiring diagram


And here is the code:

Code:
#include <LiquidCrystal.h>

// Pin assignments
const int LED_RED_PIN = 2;
const int LED_YELLOW_PIN = 3;
const int LED_GREEN_PIN = 4;
const int BUZZER_PIN = 5;
const int BUTTON_PIN = 6;
const int LCD_RS_PIN = 7;
const int LCD_EN_PIN = 8;
const int LCD_D4_PIN = 9;
const int LCD_D5_PIN = 10;
const int LCD_D6_PIN = 11;
const int LCD_D7_PIN = 12;

// LCD display constants
const int LCD_WIDTH = 16;
const int LCD_HEIGHT = 2;

// Medicine schedule constants
const int TOTAL_MEDICINES = 3;
const int MEDICINE_TIMES[TOTAL_MEDICINES][2] = {
  {8, 30},
  {13, 0},
  {19, 30}
};

// State variables
int currentMedicine = 0;
bool medicineTaken = false;

// Initialize the LCD display
LiquidCrystal lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN);

void setup() {
  // Set up the LED pins
  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_YELLOW_PIN, OUTPUT);
  pinMode(LED_GREEN_PIN, OUTPUT);
 
  // Set up the buzzer pin
  pinMode(BUZZER_PIN, OUTPUT);
 
  // Set up the button pin
  pinMode(BUTTON_PIN, INPUT_PULLUP);
 
  // Initialize the LCD display
  lcd.begin(LCD_WIDTH, LCD_HEIGHT);
 
  // Print the initial message to the LCD display
  lcd.print("Medicine Reminder");
  lcd.setCursor(0, 1);
  lcd.print("Press button");
}

void loop() {
  // Check if the button has been pressed
  if (digitalRead(BUTTON_PIN) == LOW) {
    // Reset the medicine taken state
    medicineTaken = false;
   
    // Check if it's time to take a medicine
    int hour = hour();
    int minute = minute();
    for (int i = 0; i < TOTAL_MEDICINES; i++) {
      if (hour == MEDICINE_TIMES[i][0] && minute == MEDICINE_TIMES[i][1]) {
        currentMedicine = i;
        break;
      }
    }
   
    // Display the medicine name on the LCD display
    lcd.clear();
    lcd.print("Take medicine:");
    lcd.setCursor(0, 1);
    switch (currentMedicine) {
      case 0:
        lcd.print("Medicine A");
        break;
      case 1:
        lcd.print("Medicine B");
        break;
      case 2:
        lcd.print("Medicine C");
        break;
    }
   
    // Light up the appropriate LED
    switch (currentMedicine) {
      case 0:
        digitalWrite(LED_RED_PIN, HIGH);
        break;
      case 1:
        digitalWrite(LED_YELLOW_PIN, HIGH);
        break;
      case 2:
        digitalWrite(LED_GREEN_PIN, HIGH);
        break;
    }
   
    // Play a sound to alert the user
    tone(BUZZER_PIN, 1000, 2000);
   
    // Wait for the user to press the button
    while (digitalRead(BUTTON_PIN) == HIGH) {
      // Do nothing
    }
   
    // Mark the medicine as taken
    medicineTaken = true;
   
    // Turn off the appropriate LED and clear the LCD display
    switch (currentMedicine) {
      case 0:
        digitalWrite(LED_RED_PIN, LOW);
        break;
      case 1:
        digitalWrite(LED_YELLOW_PIN, LOW);
        break;
      case 2:
        digitalWrite(LED_GREEN_PIN, LOW);
        break;
    }
    lcd.clear();
    lcd.print("Medicine taken!");
   
    // Wait for a few seconds before moving on
    delay(5000);
  }
 
  // If it's not time to take a medicine, just display the time
  if (!medicineTaken) {
    lcd.clear();
    lcd.print("Current time:");
    lcd.setCursor(0, 1);
    lcd.print(hour());
    lcd.print(":");
    if (minute() < 10) {
      lcd.print("0");
    }
    lcd.print(minute());
  }
}

Please note that the code uses the Time library, so you will need to install this library in your Arduino IDE in order to compile and upload the code to the Arduino Nano.
hour cannot be use as function error message
 
Here is the wiring diagram for the project:

medicine reminder wiring diagram


And here is the code:

Code:
#include <LiquidCrystal.h>

// Pin assignments
const int LED_RED_PIN = 2;
const int LED_YELLOW_PIN = 3;
const int LED_GREEN_PIN = 4;
const int BUZZER_PIN = 5;
const int BUTTON_PIN = 6;
const int LCD_RS_PIN = 7;
const int LCD_EN_PIN = 8;
const int LCD_D4_PIN = 9;
const int LCD_D5_PIN = 10;
const int LCD_D6_PIN = 11;
const int LCD_D7_PIN = 12;

// LCD display constants
const int LCD_WIDTH = 16;
const int LCD_HEIGHT = 2;

// Medicine schedule constants
const int TOTAL_MEDICINES = 3;
const int MEDICINE_TIMES[TOTAL_MEDICINES][2] = {
  {8, 30},
  {13, 0},
  {19, 30}
};

// State variables
int currentMedicine = 0;
bool medicineTaken = false;

// Initialize the LCD display
LiquidCrystal lcd(LCD_RS_PIN, LCD_EN_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN);

void setup() {
  // Set up the LED pins
  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_YELLOW_PIN, OUTPUT);
  pinMode(LED_GREEN_PIN, OUTPUT);
 
  // Set up the buzzer pin
  pinMode(BUZZER_PIN, OUTPUT);
 
  // Set up the button pin
  pinMode(BUTTON_PIN, INPUT_PULLUP);
 
  // Initialize the LCD display
  lcd.begin(LCD_WIDTH, LCD_HEIGHT);
 
  // Print the initial message to the LCD display
  lcd.print("Medicine Reminder");
  lcd.setCursor(0, 1);
  lcd.print("Press button");
}

void loop() {
  // Check if the button has been pressed
  if (digitalRead(BUTTON_PIN) == LOW) {
    // Reset the medicine taken state
    medicineTaken = false;
   
    // Check if it's time to take a medicine
    int hour = hour();
    int minute = minute();
    for (int i = 0; i < TOTAL_MEDICINES; i++) {
      if (hour == MEDICINE_TIMES[i][0] && minute == MEDICINE_TIMES[i][1]) {
        currentMedicine = i;
        break;
      }
    }
   
    // Display the medicine name on the LCD display
    lcd.clear();
    lcd.print("Take medicine:");
    lcd.setCursor(0, 1);
    switch (currentMedicine) {
      case 0:
        lcd.print("Medicine A");
        break;
      case 1:
        lcd.print("Medicine B");
        break;
      case 2:
        lcd.print("Medicine C");
        break;
    }
   
    // Light up the appropriate LED
    switch (currentMedicine) {
      case 0:
        digitalWrite(LED_RED_PIN, HIGH);
        break;
      case 1:
        digitalWrite(LED_YELLOW_PIN, HIGH);
        break;
      case 2:
        digitalWrite(LED_GREEN_PIN, HIGH);
        break;
    }
   
    // Play a sound to alert the user
    tone(BUZZER_PIN, 1000, 2000);
   
    // Wait for the user to press the button
    while (digitalRead(BUTTON_PIN) == HIGH) {
      // Do nothing
    }
   
    // Mark the medicine as taken
    medicineTaken = true;
   
    // Turn off the appropriate LED and clear the LCD display
    switch (currentMedicine) {
      case 0:
        digitalWrite(LED_RED_PIN, LOW);
        break;
      case 1:
        digitalWrite(LED_YELLOW_PIN, LOW);
        break;
      case 2:
        digitalWrite(LED_GREEN_PIN, LOW);
        break;
    }
    lcd.clear();
    lcd.print("Medicine taken!");
   
    // Wait for a few seconds before moving on
    delay(5000);
  }
 
  // If it's not time to take a medicine, just display the time
  if (!medicineTaken) {
    lcd.clear();
    lcd.print("Current time:");
    lcd.setCursor(0, 1);
    lcd.print(hour());
    lcd.print(":");
    if (minute() < 10) {
      lcd.print("0");
    }
    lcd.print(minute());
  }
}

Please note that the code uses the Time library, so you will need to install this library in your Arduino IDE in order to compile and upload the code to the Arduino Nano.
what is the title of tthe library
 
wiring diagram and code for medicine remiender will light up the led which medicine will you take using

arduino nano
ds3231 rtc
16x2 lcd display
1 led
1 button
 
Here's an example wiring diagram and code for a medicine reminder with an LED and a button using an Arduino Nano, DS3231 RTC, 16x2 LCD display, 1 LED, and 1 button:

Wiring Diagram:
  • Connect the DS3231 RTC to the I2C pins (A4 and A5) of the Arduino Nano
  • Connect the 16x2 LCD display to the following pins of the Arduino Nano:
- RS (Register Select) pin to digital pin 7
- EN (Enable) pin to digital pin 6
- D4 pin to digital pin 5
- D5 pin to digital pin 4
- D6 pin to digital pin 3
- D7 pin to digital pin 2
  • Connect the LED to digital pin 8 of the Arduino Nano with a 220 ohm resistor in series
  • Connect the button to digital pin 9 of the Arduino Nano with a 10k ohm resistor in series

Code:

Code:
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal.h>

#define LED_PIN 8
#define BUTTON_PIN 9

RTC_DS3231 rtc;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int ledState = LOW;
int buttonState;
int lastButtonState = LOW;
DateTime now;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  lcd.begin(16, 2);
  if (! rtc.begin()) {
    lcd.print("RTC failed!");
    while (1);
  }
  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  now = rtc.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 1);
  lcd.print("Take Medicine");

  buttonState = digitalRead(BUTTON_PIN);
  if (buttonState != lastButtonState) {
    if (buttonState == LOW) {
      ledState = !ledState;
      digitalWrite(LED_PIN, ledState);
    }
  }
  lastButtonState = buttonState;
  delay(50);
}

The code reads the current time from the DS3231 RTC and displays it on the LCD display. It also displays a message to take medicine on the second row of the display.

The button is used to toggle the LED on and off to indicate which medicine to take. When the button is pressed, the LED changes state.

Note that this is just a simple example and can be modified or expanded as needed.
 
Sure, here's a wiring diagram and code for your medicine reminder project using an Arduino Nano, 0.9 OLED display, 1 button, 1 LED, and RTC DS3231.

Wiring diagram:

Code:
OLED Display:
GND -> GND
VCC -> 5V
SCL -> A5
SDA -> A4

DS3231 RTC:
GND -> GND
VCC -> 5V
SCL -> A5
SDA -> A4

Button:
One leg -> D2
Other leg -> GND

LED:
Anode -> D3
Cathode -> GND

Code:

Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int buttonPin = 2;
int ledPin = 3;
bool buttonState = false;

void setup() {
  Serial.begin(9600);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting the time");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.clearDisplay();
  display.display();
}

void loop() {
  DateTime now = rtc.now();
  int hour = now.hour();
  int minute = now.minute();

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Current Time:");
  display.setTextSize(2);
  display.setCursor(30, 10);

  if (hour < 10) {
    display.print("0");
  }
  display.print(hour);
  display.print(":");

  if (minute < 10) {
    display.print("0");
  }
  display.print(minute);

  if (hour == 8 && minute == 0) {
    digitalWrite(ledPin, HIGH);
    display.setCursor(30, 30);
    display.setTextSize(1);
    display.print("Take your medicine!");
  } else {
    digitalWrite(ledPin, LOW);
  }

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Last Taken:");
    display.setTextSize(2);
    display.setCursor(30, 10);

    if (hour < 10) {
      display.print("0");
    }
    display.print(hour);
    display.print(":");

    if (minute < 10) {
      display.print("0");
    }
    display.print(minute);
  }

  display.display();
}

This code displays the current time on the OLED display, and turns on the LED and displays a message on the OLED when it's time to take the medicine (in this case, at 8:00 AM). When the button is pressed, it displays the last time the medicine was taken.

Make sure to include the required libraries in your Arduino IDE before uploading the code. I hope this helps!
 
Sure, here's a wiring diagram and code for your medicine reminder project using an Arduino Nano, 0.9 OLED display, 1 button, 1 LED, and RTC DS3231.

Wiring diagram:

Code:
OLED Display:
GND -> GND
VCC -> 5V
SCL -> A5
SDA -> A4

DS3231 RTC:
GND -> GND
VCC -> 5V
SCL -> A5
SDA -> A4

Button:
One leg -> D2
Other leg -> GND

LED:
Anode -> D3
Cathode -> GND

Code:

Code:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int buttonPin = 2;
int ledPin = 3;
bool buttonState = false;

void setup() {
  Serial.begin(9600);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting the time");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.clearDisplay();
  display.display();
}

void loop() {
  DateTime now = rtc.now();
  int hour = now.hour();
  int minute = now.minute();

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Current Time:");
  display.setTextSize(2);
  display.setCursor(30, 10);

  if (hour < 10) {
    display.print("0");
  }
  display.print(hour);
  display.print(":");

  if (minute < 10) {
    display.print("0");
  }
  display.print(minute);

  if (hour == 8 && minute == 0) {
    digitalWrite(ledPin, HIGH);
    display.setCursor(30, 30);
    display.setTextSize(1);
    display.print("Take your medicine!");
  } else {
    digitalWrite(ledPin, LOW);
  }

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.setTextSize(1);
    display.println("Last Taken:");
    display.setTextSize(2);
    display.setCursor(30, 10);

    if (hour < 10) {
      display.print("0");
    }
    display.print(hour);
    display.print(":");

    if (minute < 10) {
      display.print("0");
    }
    display.print(minute);
  }

  display.display();
}

This code displays the current time on the OLED display, and turns on the LED and displays a message on the OLED when it's time to take the medicine (in this case, at 8:00 AM). When the button is pressed, it displays the last time the medicine was taken.

Make sure to include the required libraries in your Arduino IDE before uploading the code. I hope this helps!
how to adjust the time interval of taking medicine
 
wiring diagram and code for medicine remiender using arduino nano,0.9 oled display, 3 button,3 led
will light up which medicine will you take with a specific time for each medicine
 
Here's a wiring diagram and sample code for a medicine reminder using an Arduino Nano, 0.9 OLED display, 3 buttons, and 3 LEDs. This project uses the RTClib and Adafruit_GFX libraries, which you can download from the Arduino library manager.

Wiring Diagram:
Code:
   OLED Display  Arduino Nano
   GND           GND
   VCC           5V
   SDA           A4
   SCL           A5
   
   Button 1      D2
   Button 2      D3
   Button 3      D4
   
   LED 1         D5
   LED 2         D6
   LED 3         D7

Sample Code:
Code:
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_ADDR   0x3C
#define OLED_SDA    A4
#define OLED_SCL    A5

#define BUTTON_1    2
#define BUTTON_2    3
#define BUTTON_3    4

#define LED_1       5
#define LED_2       6
#define LED_3       7

RTC_DS1307 rtc;

Adafruit_SSD1306 display(128, 32, &Wire, OLED_ADDR);

void setup() {
  pinMode(BUTTON_1, INPUT_PULLUP);
  pinMode(BUTTON_2, INPUT_PULLUP);
  pinMode(BUTTON_3, INPUT_PULLUP);
  
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  
  Serial.begin(9600);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop() {
  DateTime now = rtc.now();
  
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.setTextSize(2);
  display.setCursor(0,10);
  
  // Check button 1
  if (digitalRead(BUTTON_1) == LOW) {
    display.println("Take Medicine 1");
    digitalWrite(LED_1, HIGH);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
    delay(5000);
    digitalWrite(LED_1, LOW);
  }
  // Check button 2
  else if (digitalRead(BUTTON_2) == LOW) {
    display.println("Take Medicine 2");
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, HIGH);
    digitalWrite(LED_3, LOW);
    delay(5000);
    digitalWrite(LED_2, LOW);
  }
  // Check button 3
  else if (digitalRead(BUTTON_3) == LOW) {
    display.println("Take Medicine 3");
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, HIGH);
    delay(5000);
    digitalWrite(LED_3, LOW);
  }
  // Display time
  else {
    display.println(now.toString());
    digitalWrite(LED_1, LOW);
    digitalWrite(LED_2, LOW);
    digitalWrite(LED_3, LOW);
  }
  
  display.display();
  display.clearDisplay();
}

Note: This is just a sample code to give you an idea of how to implement this project. You will need to modify the code based on your specific requirements.
 
Sure, here is a sample code for a medicine reminder using Arduino Nano and 16x2 LCD:

Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int button1 = 7; // set button pin 
int button2 = 6; // set button pin 
int button3 = 8; // set button pin 
int button4 = 9; // set button pin 

int hour = 0; // set default values
int minute = 0;
int day = 0;
int interval = 0;

void setup() {
  lcd.begin(16, 2);

  pinMode(button1, INPUT_PULLUP); // set button as input
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);

  lcd.setCursor(0,0); // set cursor position
  lcd.print("MEDICINE REMINDER");
  delay(2000);
  lcd.clear();
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("SET TIME (HH:MM)");
  lcd.setCursor(0,1);
  lcd.print("Press button 1");

  // set hour and minute using buttons
  while(digitalRead(button1) == HIGH){}
  hour = hour + 1;
  delay(200);
  if(hour > 23){
    hour = 0;
  }
  lcd.setCursor(0,0);
  if(hour < 10){
    lcd.print("0");
  }
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  
  while(digitalRead(button2) == HIGH){}
  minute = minute + 1;
  delay(200);
  if(minute > 59){
    minute = 0;
  }
  lcd.setCursor(0,0);
  if(hour < 10){
    lcd.print("0");
  }
  lcd.print(hour);
  lcd.print(":");
  if(minute < 10){
    lcd.print("0");
  }
  lcd.print(minute);

  // set day interval using buttons
  lcd.setCursor(0,1);
  lcd.print("SET DAY INTERVAL");
  lcd.setCursor(0,0);
  lcd.print("Press button 3");
  while(digitalRead(button3) == HIGH){}
  day = day + 1;
  delay(200);
  if(day > 31){
    day = 1;
  }
  lcd.setCursor(0,1);
  lcd.print("Interval:");
  lcd.print(day);
  lcd.print(" day(s)");

  // display reminder message
  lcd.setCursor(0,0);
  lcd.print("MEDICINE TIME");
  lcd.setCursor(0,1);
  lcd.print("Press button 4");
  delay(1000);

  // check if it's time for reminder message
  while(digitalRead(button4) == HIGH){}
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("REMINDER");
  lcd.setCursor(0,1);
  lcd.print("Take medicine!");
  delay(3000);

  // update day interval
  interval = interval + 1;
  if(interval == day){
    interval = 0;
    lcd.clear();
  }
}

Note that this is just a sample code and you may need to modify it to fit your specific needs. Also, make sure to connect the LCD to the correct pins on the Arduino Nano board.
 
Here's a sample code for medicine reminder using Arduino Nano, 16x2 LCD, and RTC DS3231. This code displays the current time on the LCD and reminds you to take medicine at certain intervals throughout the day. You can adjust the reminder times by modifying the reminderTimes array in the code.

Code:
c++
// include libraries
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"

// initialize objects
RTC_DS3231 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// set reminder times (24-hour format)
int reminderTimes[] = {8, 12, 18}; // 8am, 12pm, 6pm

void setup() {
  // initialize serial communication
  Serial.begin(9600);
  
  // initialize LCD
  lcd.begin(16, 2);
  lcd.print("Medicine Reminder");
  
  // initialize RTC
  rtc.begin();
  
  // set RTC to current time if necessary
  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // get current time from RTC
  DateTime now = rtc.now();
  
  // format current time for display on LCD
  String timeString = now.hour() + ":" + now.minute() + ":" + now.second();
  
  // display current time on LCD
  lcd.setCursor(0, 1);
  lcd.print(timeString);
  
  // check if it's time to remind to take medicine
  for (int i = 0; i < sizeof(reminderTimes) / sizeof(int); i++) {
    if (now.hour() == reminderTimes[i]) {
      // display reminder message on LCD
      lcd.clear();
      lcd.print("Time to take med!");
      delay(5000); // display message for 5 seconds
      lcd.clear();
      lcd.print("Medicine Reminder");
      break; // exit for loop
    }
  }
  
  delay(1000); // wait 1 second before updating display
}

Note: You'll need to install the RTClib library (You do not have permission to view the full content of this post. Log in or register now.) for this code to work.
 
Here's a sample code for medicine reminder using Arduino Nano, 16x2 LCD, and RTC DS3231. This code displays the current time on the LCD and reminds you to take medicine at certain intervals throughout the day. You can adjust the reminder times by modifying the reminderTimes array in the code.

Code:
c++
// include libraries
#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"

// initialize objects
RTC_DS3231 rtc;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// set reminder times (24-hour format)
int reminderTimes[] = {8, 12, 18}; // 8am, 12pm, 6pm

void setup() {
  // initialize serial communication
  Serial.begin(9600);
 
  // initialize LCD
  lcd.begin(16, 2);
  lcd.print("Medicine Reminder");
 
  // initialize RTC
  rtc.begin();
 
  // set RTC to current time if necessary
  if (!rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // get current time from RTC
  DateTime now = rtc.now();
 
  // format current time for display on LCD
  String timeString = now.hour() + ":" + now.minute() + ":" + now.second();
 
  // display current time on LCD
  lcd.setCursor(0, 1);
  lcd.print(timeString);
 
  // check if it's time to remind to take medicine
  for (int i = 0; i < sizeof(reminderTimes) / sizeof(int); i++) {
    if (now.hour() == reminderTimes[i]) {
      // display reminder message on LCD
      lcd.clear();
      lcd.print("Time to take med!");
      delay(5000); // display message for 5 seconds
      lcd.clear();
      lcd.print("Medicine Reminder");
      break; // exit for loop
    }
  }
 
  delay(1000); // wait 1 second before updating display
}

Note: You'll need to install the RTClib library (You do not have permission to view the full content of this post. Log in or register now.) for this code to work.
test code for rtc ds3231
 
test code for rtc ds3231
Here's a simple test code for the RTC DS3231 module with Arduino Nano and 16x2 LCD.

Code:
c++
#include <Wire.h>
#include <RTClib.h> // include the RTClib library
#include <LiquidCrystal.h> // include the LiquidCrystal library

RTC_DS3231 rtc; // initialize the RTC object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object

void setup() {
  // initialize the serial communication
  Serial.begin(9600);
  
  // initialize the LCD
  lcd.begin(16, 2);
  lcd.print("RTC Test");
  
  // initialize the I2C communication
  Wire.begin();
  
  // initialize the RTC
  rtc.begin();
  
  // set the RTC time to the current time if not set
  if (!rtc.initialized()) {
    Serial.println("RTC is NOT initialized, setting time...");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // get the current time from the RTC
  DateTime now = rtc.now();
  
  // display the time on the LCD
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);
  
  // send the time to the serial monitor
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.println(now.second(), DEC);
  
  delay(1000); // wait for 1 second
}

Note that you need to install the RTClib library for this code to work. You can download it from the Arduino Library Manager or from the GitHub repository (You do not have permission to view the full content of this post. Log in or register now.). Also, make sure you have connected the RTC module to the correct pins of the Arduino Nano and the 16x2 LCD.
 
Here's a simple test code for the RTC DS3231 module with Arduino Nano and 16x2 LCD.

Code:
c++
#include <Wire.h>
#include <RTClib.h> // include the RTClib library
#include <LiquidCrystal.h> // include the LiquidCrystal library

RTC_DS3231 rtc; // initialize the RTC object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object

void setup() {
  // initialize the serial communication
  Serial.begin(9600);
 
  // initialize the LCD
  lcd.begin(16, 2);
  lcd.print("RTC Test");
 
  // initialize the I2C communication
  Wire.begin();
 
  // initialize the RTC
  rtc.begin();
 
  // set the RTC time to the current time if not set
  if (!rtc.initialized()) {
    Serial.println("RTC is NOT initialized, setting time...");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // get the current time from the RTC
  DateTime now = rtc.now();
 
  // display the time on the LCD
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);
 
  // send the time to the serial monitor
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.println(now.second(), DEC);
 
  delay(1000); // wait for 1 second
}

Note that you need to install the RTClib library for this code to work. You can download it from the Arduino Library Manager or from the GitHub repository (You do not have permission to view the full content of this post. Log in or register now.). Also, make sure you have connected the RTC module to the correct pins of the Arduino Nano and the 16x2 LCD.
wiring for this please
 
wiring for this please
Sure, here's the wiring diagram for connecting the RTC DS3231 module and 16x2 LCD to an Arduino Nano:

Arduino Nano with RTC DS3231 and 16x2 LCD Wiring Diagram


Connections for RTC DS3231 Module:
  • VCC to 5V pin of Arduino
  • GND to GND pin of Arduino
  • SDA to A4 pin of Arduino
  • SCL to A5 pin of Arduino

Connections for 16x2 LCD:
  • VSS to GND pin of Arduino
  • VDD to 5V pin of Arduino
  • VO to potentiometer (middle pin)
  • RS to digital pin 12 of Arduino
  • RW to GND pin of Arduino
  • E to digital pin 11 of Arduino
  • D4 to digital pin 5 of Arduino
  • D5 to digital pin 4 of Arduino
  • D6 to digital pin 3 of Arduino
  • D7 to digital pin 2 of Arduino
  • A to 5V pin of Arduino
  • K to GND pin of Arduino

Note that you may need to adjust the potentiometer to adjust the contrast of the LCD display.

Also, make sure to include the required libraries in your Arduino IDE: "Wire.h", "RTClib.h", and "LiquidCrystal.h".
 
Here's a simple test code for the RTC DS3231 module with Arduino Nano and 16x2 LCD.

Code:
c++
#include <Wire.h>
#include <RTClib.h> // include the RTClib library
#include <LiquidCrystal.h> // include the LiquidCrystal library

RTC_DS3231 rtc; // initialize the RTC object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object

void setup() {
  // initialize the serial communication
  Serial.begin(9600);
 
  // initialize the LCD
  lcd.begin(16, 2);
  lcd.print("RTC Test");
 
  // initialize the I2C communication
  Wire.begin();
 
  // initialize the RTC
  rtc.begin();
 
  // set the RTC time to the current time if not set
  if (!rtc.initialized()) {
    Serial.println("RTC is NOT initialized, setting time...");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // get the current time from the RTC
  DateTime now = rtc.now();
 
  // display the time on the LCD
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);
 
  // send the time to the serial monitor
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.println(now.second(), DEC);
 
  delay(1000); // wait for 1 second
}

Note that you need to install the RTClib library for this code to work. You can download it from the Arduino Library Manager or from the GitHub repository (You do not have permission to view the full content of this post. Log in or register now.). Also, make sure you have connected the RTC module to the correct pins of the Arduino Nano and the 16x2 LCD.
theres an error for this code
 
theres an error for this code
I apologize for the mistake in my previous response. Here's the corrected code:

Code:
c++
#include <Wire.h>
#include <RTClib.h> // include the RTClib library
#include <LiquidCrystal.h> // include the LiquidCrystal library

RTC_DS3231 rtc; // initialize the RTC object
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the LCD object

void setup() {
  // initialize the serial communication
  Serial.begin(9600);
 
  // initialize the LCD
  lcd.begin(16, 2);
  lcd.print("RTC Test");
 
  // initialize the I2C communication
  Wire.begin();
 
  // initialize the RTC
  rtc.begin();
 
  // set the RTC time to the current time if not set
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, setting time...");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // get the current time from the RTC
  DateTime now = rtc.now();
 
  // display the time on the LCD
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(":");
  lcd.print(now.minute(), DEC);
  lcd.print(":");
  lcd.print(now.second(), DEC);
 
  // send the time to the serial monitor
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.println(now.second(), DEC);
 
  delay(1000); // wait for 1 second
}

The error was caused by the incorrect usage of the isrunning() function. The correct usage should be if (! rtc.isrunning()) instead of if (! rtc.initialized()).

Note that you need to install the RTClib library for this code to work. You can download it from the Arduino Library Manager or from the GitHub repository (You do not have permission to view the full content of this post. Log in or register now.). Also, make sure you have connected the RTC module to the correct pins of the Arduino Nano and the 16x2 LCD.
 
Status
Not open for further replies.

About this Thread

  • 100
    Replies
  • 2K
    Views
  • 2
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,018
Guests online
1,244
Total visitors
2,262

Forum statistics

Threads
2,273,833
Posts
28,952,002
Members
1,234,976
Latest member
yuzhimeng2026
Back
Top