// hd44780 library see https://github.com/duinoWitchery/hd44780
// thehd44780 library is available through the IDE library manager
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
// if there should be an additional I2C-devices on the I2C-Bus
// which has a lower I2C-adress it might happen that this other devices I2C-adress
// will be chosen as the I2C-adress of the LCD
// if this happends add the I2C-adress to the constructor of the HD44780
// example instead of writing hd44780_I2Cexp lcd;
// add the I2C-adress hd44780_I2Cexp (0x27);
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup() {
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
lcd.print("Hello World");
lcd.setCursor(0, 1);
lcd.print("Millis ");
}
void loop() {
updateLCD();
}
void updateLCD() {
static unsigned long lcdTimer = 0;
if ( TimePeriodIsOver(lcdTimer,500) ) { // check if more than 500 milliseconds have passed by
// if 500 milliseconds HAVE passed by
lcd.setCursor(8, 1);
lcd.print(" "); // overwrite old data
lcd.setCursor(8, 1); // reset the cursor
lcd.print(millis());
}
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long & startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}