What's new

Aokiji_Master

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
 
code to display temperature and humidity on 0.9 oled display
Sure, here's some sample code for reading data from a DHT11 humidity and temperature sensor and displaying it on a 0.96" OLED display with an Arduino Uno:

Code:
#include <Adafruit_GFX.h>    // include the Adafruit graphics library
#include <Adafruit_SSD1306.h> // include the Adafruit OLED display library
#include <DHT.h>             // include the DHT library

#define OLED_RESET 4
#define SCREEN_WIDTH 128    // OLED display width, in pixels
#define SCREEN_HEIGHT 64    // OLED display height, in pixels

// OLED display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// DHT sensor object
#define DHTPIN 2          // digital pin for DHT11 data line
#define DHTTYPE DHT11     // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  // initialize the OLED display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C address of the display
  display.clearDisplay();                   // clear the display buffer
  display.display();

  // initialize the DHT sensor
  dht.begin();
}

void loop() {
  // read data from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // display the data on the OLED display
  display.clearDisplay();
  display.setTextSize(1);             // set text size to 1
  display.setTextColor(SSD1306_WHITE);// set text color to white
  display.setCursor(0, 0);            // set cursor to top left corner
  display.print("Temperature: ");
  display.print(temperature);
  display.print(" C");

  display.setCursor(0, 10);           // set cursor to second line
  display.print("Humidity: ");
  display.print(humidity);
  display.print(" %");

  display.display();                  // update the OLED display
  delay(2000);                         // wait for 2 seconds before updating again
}

This code uses the Adafruit graphics and OLED display libraries to initialize and display data on a 0.96" OLED display. It also uses the DHT library to read temperature and humidity data from the DHT11 sensor connected to pin 2 of the Arduino. The temperature and humidity data is then displayed on the OLED display in Celsius and percentages, respectively. The loop() function continuously reads and displays data every 2 seconds. Note that you may need to adjust the pin numbers and sensor type in the code to match your specific setup.
 
Here's the code for your medicine reminder using Arduino Nano, 0.9 OLED display, 3 buttons, 3 LED lights, and RTC DS3231. Before I provide the code, please make sure that you have installed the necessary libraries for OLED display and RTC DS3231.

Code:

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

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int button1 = 2;
int button2 = 3;
int button3 = 4;

int led1 = 5;
int led2 = 6;
int led3 = 7;

int button1State = 0;
int button2State = 0;
int button3State = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  Serial.begin(9600);

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

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

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.display();

}

void loop() {
  DateTime now = rtc.now();

  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
  button3State = digitalRead(button3);

  if (button1State == HIGH) {
    digitalWrite(led1, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 1");
    display.display();
  } 
  else {
    digitalWrite(led1, LOW);
  }

  if (button2State == HIGH) {
    digitalWrite(led2, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 2");
    display.display();
  } 
  else {
    digitalWrite(led2, LOW);
  }

  if (button3State == HIGH) {
    digitalWrite(led3, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 3");
    display.display();
  } 
  else {
    digitalWrite(led3, LOW);
  }
  delay(1000);

}

Wiring Diagram:

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

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

Buttons
-------
Button 1 -> Pin 2
Button 2 -> Pin 3
Button 3 -> Pin 4

LED Lights
----------
LED 1 -> Pin 5
LED 2 -> Pin 6
LED 3 -> Pin 7

Please note that this code and wiring diagram are just examples, and you may need to modify them to fit your specific needs.
 
Here's the code for your medicine reminder using Arduino Nano, 0.9 OLED display, 3 buttons, 3 LED lights, and RTC DS3231. Before I provide the code, please make sure that you have installed the necessary libraries for OLED display and RTC DS3231.

Code:

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

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int button1 = 2;
int button2 = 3;
int button3 = 4;

int led1 = 5;
int led2 = 6;
int led3 = 7;

int button1State = 0;
int button2State = 0;
int button3State = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  Serial.begin(9600);

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

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

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.display();

}

void loop() {
  DateTime now = rtc.now();

  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
  button3State = digitalRead(button3);

  if (button1State == HIGH) {
    digitalWrite(led1, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 1");
    display.display();
  }
  else {
    digitalWrite(led1, LOW);
  }

  if (button2State == HIGH) {
    digitalWrite(led2, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 2");
    display.display();
  }
  else {
    digitalWrite(led2, LOW);
  }

  if (button3State == HIGH) {
    digitalWrite(led3, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 3");
    display.display();
  }
  else {
    digitalWrite(led3, LOW);
  }
  delay(1000);

}

Wiring Diagram:

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

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

Buttons
-------
Button 1 -> Pin 2
Button 2 -> Pin 3
Button 3 -> Pin 4

LED Lights
----------
LED 1 -> Pin 5
LED 2 -> Pin 6
LED 3 -> Pin 7

Please note that this code and wiring diagram are just examples, and you may need to modify them to fit your specific needs.
adjust the time and date for the given code
 
adjust the time and date for the given code
Sure, you can adjust the time and date for the given code using the rtc.adjust(DateTime(year, month, day, hour, minute, second)) function.

Here's an example code to set the time and date for the RTC DS3231:

Code:
void setDateTime() {
  // Call this function in the setup() to set the time and date
  rtc.adjust(DateTime(2021, 9, 1, 12, 0, 0)); // set the time and date
}

In this example, the time and date are set to September 1, 2021, 12:00:00 pm.

You can modify the parameters of the DateTime function to set the desired time and date.

Here's the modified code with the setDateTime function:

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

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int button1 = 2;
int button2 = 3;
int button3 = 4;

int led1 = 5;
int led2 = 6;
int led3 = 7;

int button1State = 0;
int button2State = 0;
int button3State = 0;

void setDateTime() {
  // Call this function in the setup() to set the time and date
  rtc.adjust(DateTime(2021, 9, 1, 12, 0, 0)); // set the time and date
}

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  Serial.begin(9600);

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    setDateTime(); // set the time and date if the RTC lost power
  }

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.display();
}

void loop() {
  DateTime now = rtc.now();

  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
  button3State = digitalRead(button3);

  if (button1State == HIGH) {
    digitalWrite(led1, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 1");
    display.display();
  }
  else {
    digitalWrite(led1, LOW);
  }

  if (button2State == HIGH) {
    digitalWrite(led2, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 2");
    display.display();
  }
  else {
    digitalWrite(led2, LOW);
  }

  if (button3State == HIGH) {
    digitalWrite(led3, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 3");
    display.display();
  }
  else {
    digitalWrite(led3, LOW);
  }
  delay(1000);
}

I hope this helps!
 
Here's the code for your medicine reminder using Arduino Nano, 0.9 OLED display, 3 buttons, 3 LED lights, and RTC DS3231. Before I provide the code, please make sure that you have installed the necessary libraries for OLED display and RTC DS3231.

Code:

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

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int button1 = 2;
int button2 = 3;
int button3 = 4;

int led1 = 5;
int led2 = 6;
int led3 = 7;

int button1State = 0;
int button2State = 0;
int button3State = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  Serial.begin(9600);

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

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

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.display();

}

void loop() {
  DateTime now = rtc.now();

  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
  button3State = digitalRead(button3);

  if (button1State == HIGH) {
    digitalWrite(led1, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 1");
    display.display();
  }
  else {
    digitalWrite(led1, LOW);
  }

  if (button2State == HIGH) {
    digitalWrite(led2, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 2");
    display.display();
  }
  else {
    digitalWrite(led2, LOW);
  }

  if (button3State == HIGH) {
    digitalWrite(led3, HIGH);
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("It's time to take");
    display.println("Medicine 3");
    display.display();
  }
  else {
    digitalWrite(led3, LOW);
  }
  delay(1000);

}

Wiring Diagram:

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

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

Buttons
-------
Button 1 -> Pin 2
Button 2 -> Pin 3
Button 3 -> Pin 4

LED Lights
----------
LED 1 -> Pin 5
LED 2 -> Pin 6
LED 3 -> Pin 7

Please note that this code and wiring diagram are just examples, and you may need to modify them to fit your specific needs.
display each medicine 10 seconds and then the other one will flash and so on
 
incomplete code
I apologize for the mistake in my previous response. Here's the modified code that displays each medicine for 10 seconds and switches to the next medicine when the time is up:

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

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int button1 = 2;
int button2 = 3;
int button3 = 4;

int led1 = 5;
int led2 = 6;
int led3 = 7;

int button1State = 0;
int button2State = 0;
int button3State = 0;

unsigned long previousMillis = 0;
const long interval = 10000; // display each medicine for 10 seconds

int currentMedicine = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  Serial.begin(9600);

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F([B]DATE[/B]), F([B]TIME[/B])));
  }

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.display();

}

void loop() {
  DateTime now = rtc.now();

  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
  button3State = digitalRead(button3);

  if (button1State == HIGH) {
    currentMedicine = 1;
  }
  else if (button2State == HIGH) {
    currentMedicine = 2;
  }
  else if (button3State == HIGH) {
    currentMedicine = 3;
  }

  switch(currentMedicine) {
    case 1:
      digitalWrite(led1, HIGH);
      display.clearDisplay();
      display.setCursor(0,0);
      display.println("It's time to take");
      display.println("Medicine 1");
      display.display();
      break;
    case 2:
      digitalWrite(led2, HIGH);
      display.clearDisplay();
      display.setCursor(0,0);
      display.println("It's time to take");
      display.println("Medicine 2");
      display.display();
      break;
    case 3:
      digitalWrite(led3, HIGH);
      display.clearDisplay();
      display.setCursor(0,0);
      display.println("It's time to take");
      display.println("Medicine 3");
      display.display();
      break;
    default:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      display.clearDisplay();
 
I apologize for the mistake in my previous response. Here's the modified code that displays each medicine for 10 seconds and switches to the next medicine when the time is up:

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

#define OLED_RESET     4
Adafruit_SSD1306 display(OLED_RESET);

RTC_DS3231 rtc;

int button1 = 2;
int button2 = 3;
int button3 = 4;

int led1 = 5;
int led2 = 6;
int led3 = 7;

int button1State = 0;
int button2State = 0;
int button3State = 0;

unsigned long previousMillis = 0;
const long interval = 10000; // display each medicine for 10 seconds

int currentMedicine = 0;

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  Serial.begin(9600);

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

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    rtc.adjust(DateTime(F([B]DATE[/B]), F([B]TIME[/B])));
  }

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.println("Medicine Reminder");
  display.display();

}

void loop() {
  DateTime now = rtc.now();

  button1State = digitalRead(button1);
  button2State = digitalRead(button2);
  button3State = digitalRead(button3);

  if (button1State == HIGH) {
    currentMedicine = 1;
  }
  else if (button2State == HIGH) {
    currentMedicine = 2;
  }
  else if (button3State == HIGH) {
    currentMedicine = 3;
  }

  switch(currentMedicine) {
    case 1:
      digitalWrite(led1, HIGH);
      display.clearDisplay();
      display.setCursor(0,0);
      display.println("It's time to take");
      display.println("Medicine 1");
      display.display();
      break;
    case 2:
      digitalWrite(led2, HIGH);
      display.clearDisplay();
      display.setCursor(0,0);
      display.println("It's time to take");
      display.println("Medicine 2");
      display.display();
      break;
    case 3:
      digitalWrite(led3, HIGH);
      display.clearDisplay();
      display.setCursor(0,0);
      display.println("It's time to take");
      display.println("Medicine 3");
      display.display();
      break;
    default:
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      display.clearDisplay();
how to adjust the date and time example
 
Here is the wiring diagram and code for a medicine reminder using an Arduino Nano, RTC DS3231, 3 LEDs, 3 buttons, and a 16x2 LCD display. Please note that this is just an example and you may need to modify the code and wiring to fit your specific needs.

Wiring diagram:
Code:
RTC DS3231:
   SDA - A4
   SCL - A5
   VCC - 5V
   GND - GND

LCD Display:
   RS - D12
   E - D11
   D4 - D5
   D5 - D4
   D6 - D3
   D7 - D2
   VCC - 5V
   GND - GND

LED1:
   Anode - D8
   Cathode - GND

LED2:
   Anode - D9
   Cathode - GND

LED3:
   Anode - D10
   Cathode - GND

Button1:
   Pin - D7
   One leg connected to GND

Button2:
   Pin - D6
   One leg connected to GND

Button3:
   Pin - D5
   One leg connected to GND

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

// Initialize RTC and LCD
DS3231 rtc(SDA, SCL);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DateTime now;

// Initialize LEDs and Buttons
const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int button1 = 7;
const int button2 = 6;
const int button3 = 5;

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

  // Initialize LEDs
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  // Initialize Buttons
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);

  // Initialize RTC
  rtc.begin();
  rtc.setDateTime(__DATE__, __TIME__);
}

void loop() {
  now = rtc.now();

  // Display current date and time on LCD
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.print(' ');
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);

  // Check if it's time for medicine 1
  if (now.hour() == 8 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
  }

  // Check if it's time for medicine 2
  if (now.hour() == 12 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led2, HIGH);
    delay(1000);
    digitalWrite(led2, LOW);
  }

  // Check if it's time for medicine 3
  if (now.hour() == 16 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led3, LOW);
  }

  // Check if button1 is pressed
  if (digitalRead(button1) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 1 pressed");
    delay(1000);
    lcd.clear();
  }

  // Check if button2 is pressed
  if (digitalRead(button2) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 2 pressed");
    delay(1000);
    lcd.clear();
  }

  // Check if button3 is pressed
  if (digitalRead(button3) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 3 pressed");
    delay(1000);
    lcd.clear();
  }
}

This code should display the current date and time on the LCD and light up a corresponding LED when it's time to take a medicine. It also checks for button presses and displays a message on the LCD if a button is pressed. You can modify this code to fit your specific needs.
 
Here is the wiring diagram and code for a medicine reminder using an Arduino Nano, RTC DS3231, 3 LEDs, 3 buttons, and a 16x2 LCD display. Please note that this is just an example and you may need to modify the code and wiring to fit your specific needs.

Wiring diagram:
Code:
RTC DS3231:
   SDA - A4
   SCL - A5
   VCC - 5V
   GND - GND

LCD Display:
   RS - D12
   E - D11
   D4 - D5
   D5 - D4
   D6 - D3
   D7 - D2
   VCC - 5V
   GND - GND

LED1:
   Anode - D8
   Cathode - GND

LED2:
   Anode - D9
   Cathode - GND

LED3:
   Anode - D10
   Cathode - GND

Button1:
   Pin - D7
   One leg connected to GND

Button2:
   Pin - D6
   One leg connected to GND

Button3:
   Pin - D5
   One leg connected to GND

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

// Initialize RTC and LCD
DS3231 rtc(SDA, SCL);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DateTime now;

// Initialize LEDs and Buttons
const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int button1 = 7;
const int button2 = 6;
const int button3 = 5;

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

  // Initialize LEDs
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  // Initialize Buttons
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);

  // Initialize RTC
  rtc.begin();
  rtc.setDateTime(__DATE__, __TIME__);
}

void loop() {
  now = rtc.now();

  // Display current date and time on LCD
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.print(' ');
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);

  // Check if it's time for medicine 1
  if (now.hour() == 8 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
  }

  // Check if it's time for medicine 2
  if (now.hour() == 12 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led2, HIGH);
    delay(1000);
    digitalWrite(led2, LOW);
  }

  // Check if it's time for medicine 3
  if (now.hour() == 16 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led3, LOW);
  }

  // Check if button1 is pressed
  if (digitalRead(button1) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 1 pressed");
    delay(1000);
    lcd.clear();
  }

  // Check if button2 is pressed
  if (digitalRead(button2) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 2 pressed");
    delay(1000);
    lcd.clear();
  }

  // Check if button3 is pressed
  if (digitalRead(button3) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 3 pressed");
    delay(1000);
    lcd.clear();
  }
}

This code should display the current date and time on the LCD and light up a corresponding LED when it's time to take a medicine. It also checks for button presses and displays a message on the LCD if a button is pressed. You can modify this code to fit your specific needs.
wiring of V0 of 16x2 lcd to arduino nano using potentiommeter
 
wiring of V0 of 16x2 lcd to arduino nano using potentiommeter
To connect the V0 pin of the 16x2 LCD display to the Arduino Nano using a potentiometer, you can follow these steps:

1. Locate the V0 pin on the 16x2 LCD display. This pin controls the contrast of the display.

2. Connect the V0 pin to the center pin of the potentiometer.

3. Connect one of the outer pins of the potentiometer to ground (GND) on the Arduino Nano.

4. Connect the other outer pin of the potentiometer to 5V on the Arduino Nano.

5. Adjust the potentiometer to vary the voltage on the V0 pin and adjust the contrast of the display.

Here is a wiring diagram to help you connect the V0 pin of the 16x2 LCD display to the Arduino Nano using a potentiometer:

[Arduino Nano] - [Potentiometer] - [16x2 LCD Display]
GND - Outer pin - GND
5V - Outer pin - VCC
A0 - Center pin - V0

Note: A0 is used as an analog input to read the voltage coming from the potentiometer.

Once you have connected the V0 pin of the 16x2 LCD display to the Arduino Nano using a potentiometer, you can use the following code to adjust the contrast:

Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int contrast = A0;

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

void loop() {
  int value = analogRead(contrast);
  lcd.setCursor(0, 0);
  lcd.print("Contrast:");
  lcd.setCursor(0, 1);
  lcd.print(value);
  delay(100);
}

This code reads the voltage coming from the potentiometer and displays it on the LCD display. You can adjust the potentiometer to vary the voltage and adjust the contrast of the display.
 
Here is the wiring diagram and code for a medicine reminder using an Arduino Nano, RTC DS3231, 3 LEDs, 3 buttons, and a 16x2 LCD display. Please note that this is just an example and you may need to modify the code and wiring to fit your specific needs.

Wiring diagram:
Code:
RTC DS3231:
   SDA - A4
   SCL - A5
   VCC - 5V
   GND - GND

LCD Display:
   RS - D12
   E - D11
   D4 - D5
   D5 - D4
   D6 - D3
   D7 - D2
   VCC - 5V
   GND - GND

LED1:
   Anode - D8
   Cathode - GND

LED2:
   Anode - D9
   Cathode - GND

LED3:
   Anode - D10
   Cathode - GND

Button1:
   Pin - D7
   One leg connected to GND

Button2:
   Pin - D6
   One leg connected to GND

Button3:
   Pin - D5
   One leg connected to GND

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

// Initialize RTC and LCD
DS3231 rtc(SDA, SCL);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DateTime now;

// Initialize LEDs and Buttons
const int led1 = 8;
const int led2 = 9;
const int led3 = 10;
const int button1 = 7;
const int button2 = 6;
const int button3 = 5;

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

  // Initialize LEDs
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);

  // Initialize Buttons
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);

  // Initialize RTC
  rtc.begin();
  rtc.setDateTime(__DATE__, __TIME__);
}

void loop() {
  now = rtc.now();

  // Display current date and time on LCD
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.print(' ');
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);

  // Check if it's time for medicine 1
  if (now.hour() == 8 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led1, HIGH);
    delay(1000);
    digitalWrite(led1, LOW);
  }

  // Check if it's time for medicine 2
  if (now.hour() == 12 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led2, HIGH);
    delay(1000);
    digitalWrite(led2, LOW);
  }

  // Check if it's time for medicine 3
  if (now.hour() == 16 && now.minute() == 0 && now.second() == 0) {
    digitalWrite(led3, HIGH);
    delay(1000);
    digitalWrite(led3, LOW);
  }

  // Check if button1 is pressed
  if (digitalRead(button1) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 1 pressed");
    delay(1000);
    lcd.clear();
  }

  // Check if button2 is pressed
  if (digitalRead(button2) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 2 pressed");
    delay(1000);
    lcd.clear();
  }

  // Check if button3 is pressed
  if (digitalRead(button3) == LOW) {
    lcd.setCursor(0, 1);
    lcd.print("Button 3 pressed");
    delay(1000);
    lcd.clear();
  }
}

This code should display the current date and time on the LCD and light up a corresponding LED when it's time to take a medicine. It also checks for button presses and displays a message on the LCD if a button is pressed. You can modify this code to fit your specific needs
ds 3231 no such file directory
 
Here's an example code for a medicine reminder using an Arduino Nano, a 16x2 LCD display, 3 buttons, 3 LEDs, and a buzzer. The code displays the current time on the LCD and allows the user to set up to three reminder times. At each reminder time, the corresponding LED will light up and the buzzer will sound until the user presses the corresponding button to acknowledge that they have taken their medicine.

Before uploading the code, make sure to connect the components according to the wiring diagram shown below.

Code:
// include libraries
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// define pins for components
const int BUTTON_1_PIN = 2;
const int BUTTON_2_PIN = 3;
const int BUTTON_3_PIN = 4;
const int LED_1_PIN = 5;
const int LED_2_PIN = 6;
const int LED_3_PIN = 7;
const int BUZZER_PIN = 8;

// define variables for reminder times
int hour1;
int minute1;
int hour2;
int minute2;
int hour3;
int minute3;

// create an instance of the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // initialize pins for buttons, LEDs, and buzzer
  pinMode(BUTTON_1_PIN, INPUT_PULLUP);
  pinMode(BUTTON_2_PIN, INPUT_PULLUP);
  pinMode(BUTTON_3_PIN, INPUT_PULLUP);
  pinMode(LED_1_PIN, OUTPUT);
  pinMode(LED_2_PIN, OUTPUT);
  pinMode(LED_3_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // initialize the LCD display
  lcd.init();
  lcd.backlight();

  // set default reminder times to 9am, 1pm, and 5pm
  hour1 = 9;
  minute1 = 0;
  hour2 = 13;
  minute2 = 0;
  hour3 = 17;
  minute3 = 0;
}

void loop() {
  // get the current time
  int currentHour = hour();
  int currentMinute = minute();
  int currentSecond = second();

  // display the current time on the LCD
  lcd.setCursor(0, 0);
  lcd.print("Time: ");
  lcd.print(currentHour);
  lcd.print(":");
  if (currentMinute < 10) {
    lcd.print("0");
  }
  lcd.print(currentMinute);
  lcd.print(":");
  if (currentSecond < 10) {
    lcd.print("0");
  }
  lcd.print(currentSecond);

  // check if it's time for any reminders
  if (currentHour == hour1 && currentMinute == minute1) {
    digitalWrite(LED_1_PIN, HIGH);
    tone(BUZZER_PIN, 1000);
  }
  else {
    digitalWrite(LED_1_PIN, LOW);
    noTone(BUZZER_PIN);
  }

  if (currentHour == hour2 && currentMinute == minute2) {
    digitalWrite(LED_2_PIN, HIGH);
    tone(BUZZER_PIN, 1000);
  }
  else {
    digitalWrite(LED_2_PIN, LOW);
    noTone(BUZZER_PIN);
  }

  if (currentHour == hour3 && currentMinute == minute3) {
    digitalWrite(LED_3_PIN, HIGH);
    tone(BUZZER_PIN, 1000);
  }
  else {
    digitalWrite(LED_3_PIN, LOW);
    noTone(BUZZER_PIN);
  }

  // check if any buttons have been pressed
  if (digitalRead(BUTTON_1_PIN) == LOW) {
    hour1 = setReminder("Reminder 1");
  }

  if (digitalRead(BUTTON_2_PIN) == LOW) {
    hour2 = setReminder("Reminder 2");
  }

  if (digitalRead(BUTTON_3_PIN) == LOW) {
    hour3 = setReminder("Reminder 3");
  }
}

// function to set a reminder time from the user
int setReminder(String reminderName) {
  // display the current reminder time on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(reminderName + ": ");
  lcd.print(hour1);
  lcd.print(":");
  if (minute1 < 10) {
    lcd.print("0");
  }
  lcd.print(minute1);

  // wait for the user to press a button
  while (true) {
    if (digitalRead(BUTTON_1_PIN) == LOW) {
      minute1 += 15;
      if (minute1 >= 60) {
        minute1 = 0;
        hour1 += 1;
        if (hour1 >= 24) {
          hour1 = 0;
        }
      }
      lcd.setCursor(0, 1);
      lcd.print("New time: ");
      lcd.print(hour1);
      lcd.print(":");
      if (minute1 < 10) {
        lcd.print("0");
      }
      lcd.print(minute1);
      delay(100);
    }
    else if (digitalRead(BUTTON_2_PIN) == LOW) {
      return hour1;
    }
    else if (digitalRead(BUTTON_3_PIN) == LOW) {
      break;
    }
  }

  // wait for the user to release the button
  while (digitalRead(BUTTON_3_PIN) == LOW) {}

  // return the new reminder time
  return hour1;
}

Here's the wiring diagram for the components:

Code:
LCD display:
- SDA pin -> A4
- SCL pin -> A5
- VCC -> 5V
- GND -> GND

Buttons:
- One pin of each button connected to 5V
- Other pin of each button connected to BUTTON_1_PIN, BUTTON_2_PIN, and BUTTON_3_PIN, respectively
- Each button pin also connected to a 10K ohm resistor, which is connected to GND

LEDs:
- One pin of each LED connected to a digital pin (LED_1_PIN, LED_2_PIN, and LED_3_PIN, respectively)
- Other pin of each LED connected to a 220 ohm resistor, which is connected to GND

Buzzer:
- Positive pin connected to BUZZER_PIN
- Negative pin connected to GND
 
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.
 
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.
wiring please
 

Similar threads

Back
Top