I have uploaded a sketch to an Arduino Uno and connected the 16x2 display directly to the Arduino using I2C .
While other sketches will show up in the display, this one won't. the leds blink / flash during the upload, so I'm fairly sure it went ok. .
This is the sketch.
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & config exapander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
int pushVal = 0;
int val;
int val2;
int addr = 0;
int isStopped = 0;
RTC_DS3231 rtc;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; // lcd pins
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define getWellsoon 0
#define HELP_SCREEN 1
#define TIME_SCREEN 2
int isFirstDone = 0, isSecondDone = 0, isThirdDone = 0;
int ledPin = LED_BUILTIN; // buzzer and led pin
int ledState = LOW;
int Signal = 0;
int buzz = 13;
int push1state, push2state, push3state, stopinState = 0;
int push1Flag, push2Flag, Push3Flag = false; // push button flags
int push1pin = 9;
int push2pin = 8;
int push3pin = 7;
int stopPin = 10;
int screens = 0; // screens to show
int maxScreen = 2; // screen count
bool isScreenChanged = true;
long previousMillis = 0;
long interval = 250; // buzzing interval
unsigned long currentMillis;
long previousMillisLCD = 0; // for LCD screen update
long intervalLCD = 2000; // Screen cycling interval
unsigned long currentMillisLCD;
// Set Reminder Change Time
// ##Set these for reminder time in 24hr Format
int buzz8amHH = 16; // HH - hours
int buzz8amMM = 30; // MM - Minute
int buzz8amSS = 00; // SS - Seconds
int buzz2pmHH = 16; // HH - hours
int buzz2pmMM = 30; // MM - Minute
int buzz2pmSS = 30; // SS - Seconds
int buzz8pmHH = 14; // HH - hours
int buzz8pmMM = 31; // MM - Minute
int buzz8pmSS = 00; // SS - Seconds
int nowHr, nowMin, nowSec; // to show current mm,hh,ss
void gwsMessege(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" The Greatest ");
lcd.setCursor(0, 1);
lcd.print("Wealth is Health");
}
void helpScreen() { // function to display 1st screen in LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Buttons to");
lcd.setCursor(0, 1);
lcd.print("set 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);
}
void setup() {
Serial.begin(9600); // start serial debugging
if (! rtc.begin()) { // check if rtc is connected
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__))); //real time set
rtc.adjust(DateTime(2019, 4, 9, 16, 29, 50)); // manual time set
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome to IIIT"); // print a messege at startup
lcd.setCursor(0, 1);
lcd.print(" BHUBANESWAR");
delay(1000);
pinMode(push1pin, INPUT); // define push button pins type
pinMode(push2pin, INPUT);
pinMode(push3pin, INPUT);
pinMode(stopPin, INPUT);
pinMode(ledPin, OUTPUT);
delay(200);
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:
Serial.println("Set for 1/day");
push1state = 1;
push2state = 0;
push3state = 0;
pushVal = 1;
break;
case 2:
Serial.println("Set for 2/day");
push1state = 0;
push2state = 1;
push3state = 0;
pushVal = 2;
break;
case 3:
Serial.println("Set for 3/day");
push1state = 0;
push2state = 0;
push3state = 1;
pushVal = 3;
break;
}
}
void loop() {
push1(); //call to set once/day
push2(); //call to set twice/day
push3(); //call to set thrice/day
if (pushVal == 1) {
at8am(); //function to start buzzing at 8am
}
else if (pushVal == 2) { // if push button 2 pressed then remind at 8am and 8pm
if(!isFirstDone || isStopped)
at8am();
at8pm();
}
else if (pushVal == 3) { // if push button 3 pressed then remind at 8am, 2pm and 8pm
if(!isFirstDone || isStopped)
at8am();
if(!isSecondDone || isStopped)
at2pm();
at8pm();
}
push1state = digitalRead(push1pin); // start reading all push button pins
push2state = digitalRead(push2pin);
push3state = digitalRead(push3pin);
stopinState = digitalRead(stopPin);
stopPins();
changeScreen();
}
// push buttons
void push1() { // function to set reminder once/day
if (push1state == 1) {
push1state = 0;
push2state = 0;
push3state = 0;
EEPROM.write(addr, 1);
Serial.print("Push1 Written : "); Serial.println(EEPROM.read(addr));
// for debugging save the state of push button-1
pushVal = 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
if (push2state == 1) {
push2state = 0;
push1state = 0;
push3state = 0;
EEPROM.write(addr, 2);
Serial.print("Push2 Written : "); Serial.println(EEPROM.read(addr));
pushVal = 2;
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
if (push3state == 1) {
push3state = 0;
push1state = 0;
push2state = 0;
EEPROM.write(addr, 3);
Serial.print("Push3 Written : "); Serial.println(EEPROM.read(addr));
pushVal = 3;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Reminder set ");
lcd.setCursor(0, 1);
lcd.print("for Thrice/day !");
delay(1200);
lcd.clear();
}
}
void stopPins() { //function to stop buzzing when user pushes stop push button
if (stopinState == 1) {
isStopped = 1;
if(isThirdTime())
isFirstDone = isSecondDone = isThirdDone = 1;
else if(isSecondTime())
isFirstDone = isSecondDone = 1;
else if(isFirstTime())
isFirstDone = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Take Medicine ");
lcd.setCursor(0, 1);
lcd.print("with Warm Water");
delay(1200);
lcd.clear();
}
}
void startBuzz(int isCurrentDone) { // function to start buzzing when time reaches to defined interval
if (isCurrentDone == 0) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // save the last time you blinked the LED
if (ledState == LOW) { // if the LED is off turn it on and vice-versa:
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
else {
ledState = LOW;
digitalWrite(ledPin, ledState);
isStopped = 0;
}
}
bool isFirstTime() {
DateTime now = rtc.now();
if (int(now.hour()) >= buzz8amHH)
if (int(now.minute()) >= buzz8amMM)
if (int(now.second()) > buzz8amSS)
return true;
return false;
}
bool isSecondTime() {
DateTime now = rtc.now();
if (int(now.hour()) >= buzz2pmHH)
if (int(now.minute()) >= buzz2pmMM)
if (int(now.second()) > buzz2pmSS)
return true;
return false;
}
bool isThirdTime() {
DateTime now = rtc.now();
if (int(now.hour()) >= buzz8pmHH)
if (int(now.minute()) >= buzz8pmMM)
if (int(now.second()) > buzz8pmSS)
return true;
return false;
}
void at8am() {
if(isFirstTime()) {
startBuzz(isFirstDone);
}
}
void at2pm() {
if(isSecondTime()) {
startBuzz(isSecondDone);
}
}
void at8pm() {
if(isThirdTime()) {
startBuzz(isThirdDone);
}
}
void changeScreen() {
// Start switching screen every defined intervalLCD
currentMillisLCD = millis();
if (currentMillisLCD - previousMillisLCD > intervalLCD) // save the last time you changed the display
{
previousMillisLCD = currentMillisLCD;
screens++;
if (screens > maxScreen) {
screens = 0; // all screens over -> start from 1st
}
isScreenChanged = true;
}
// Start displaying current screen
if (isScreenChanged) // only update the screen if the screen is changed.
{
isScreenChanged = false; // reset for next iteration
switch (screens)
{
case getWellsoon:
gwsMessege();
break;
case HELP_SCREEN:
helpScreen();
break;
case TIME_SCREEN:
timeScreen();
break;
default:
//NOT SET.
break;
}
}
}