// For: https://forum.arduino.cc/t/i2c-display-regular-display-dont-work-together/1048684
// This Wokwi project: https://wokwi.com/projects/347227935571182164
//
// 2 nov 2022
//
// Using the excellent "hd44780" library.
// The library is in the Library Manager of the Arduino IDE.
//
// For explanation and details of the library and the sketch, see these links:
// Library: https://github.com/duinoWitchery/hd44780
// Using pinIO example: https://github.com/duinoWitchery/hd44780/blob/master/examples/ioClass/hd44780_pinIO/HelloWorld/HelloWorld.ino
// Using I2C example: https://github.com/duinoWitchery/hd44780/blob/master/examples/ioClass/hd44780_I2Cexp/HelloWorld/HelloWorld.ino
//
// The public domain UpTime() function was taken from:
// https://github.com/duinoWitchery/hd44780/blob/master/examples/ioClass/hd44780_pinIO/UpTime/UpTime.ino
//
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_pinIO.h> // Arduino pin i/o class header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
const int rs=8, en=9, db4=4, db5=5, db6=6, db7=7;
hd44780_pinIO lcd1(rs, en, db4, db5, db6, db7); // lcd object for pin I/O
hd44780_I2Cexp lcd2; // declare lcd object: auto locate & auto config expander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup()
{
Serial.begin(115200); // Set the Serial Monitor of the Arduino IDE also to 115200
Serial.println( "The sketch has started");
int status = lcd1.begin(LCD_COLS, LCD_ROWS);
if(status) // non zero status means it was unsuccesful
{
Serial.println( "The pinIO LCD failed.");
// hd44780 has a fatalError() routine that blinks an led if possible
// begin() failed so blink error code using the onboard LED if possible
hd44780::fatalError(status); // does not return
}
status = lcd2.begin(LCD_COLS, LCD_ROWS);
if(status) // non zero status means it was unsuccesful
{
Serial.println( "The I2C LCD failed.");
// hd44780 has a fatalError() routine that blinks an led if possible
// begin() failed so blink error code using the onboard LED if possible
hd44780::fatalError(status); // does not return
}
// Print a message to the LCD
lcd1.print("Hello LCD One");
lcd2.print("Hello LCD Two");
// Optional extra: the library can do line wrap.
lcd2.lineWrap();
lcd2.setCursor(0, 0);
lcd2.print("Optional extra: line wrapping.");
}
void loop()
{
static unsigned long lastsecs = -1; // pre-initialize with non zero value
unsigned long secs;
int status;
secs = millis() / 1000;
// see if 1 second has passed
// so the display is only updated once per second
if(secs != lastsecs)
{
lastsecs = secs; // keep track of last seconds
lcd1.setCursor(0, 1);
// print uptime on lcd device: (time since last reset)
PrintUpTime(lcd1, secs);
}
}
// PrintUpTime(outdev, secs) - print uptime in HH:MM:SS format
// outdev - the device to send output
// secs - the total number of seconds uptime
//
// This is a fancy output routine.
// outdev is a Print class object which indicates
// where the output should be sent.
// PrintUpTime can be used with any object that uses the Print class.
// This code works with Serial objects, as well as the the hd44780 lcd objects.
// i.e. you can call with Serial: PrintUpTime(Serial, seconds);
void PrintUpTime(Print &outdev, unsigned long secs)
{
unsigned int hr, mins, sec;
// convert total seconds to hours, mins, seconds
mins = secs / 60; // how many total minutes
hr = mins / 60; // how many total hours
mins = mins % 60; // how many minutes within the hour
sec = secs % 60; // how many seconds within the minute
// print uptime in HH:MM:SS format
if(hr > 99)
hr %= 100; // limit hr to 0-99
// Print class does not support fixed width formatting
// so insert a zero if number smaller than 10
if(hr < 10)
outdev.write('0');
outdev.print((int)hr);
outdev.write(':');
if(mins < 10)
outdev.write('0');
outdev.print((int)mins);
outdev.write(':');
if(sec < 10)
outdev.write('0');
outdev.print((int)sec);
}