LCD Display
lcd_display.ino
/*
Project: Smart Home
Component: LCD with I2C Backpack
Sketch Description: This sketch has functions to display static and scrolling text on a Liquid Crystal Display.
Author: STEMVentor Educonsulting
This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use.
*/
// LIBRARIES
// For communicating with I2C devices. Pre-installed with IDE.
#include <Wire.h>
//https://www.arduino.cc/reference/en/libraries/liquidcrystal-i2c/
// Install the library "LiquidCrystal I2C by Frank de Brabander" from the IDE library manager.
#include <LiquidCrystal_I2C.h>
// PIN DEFINITIONS
// The I2C LCD is connected to the SDA and SCK pins on the board
// so do not have to be explictly defined.
// GLOBAL CONSTANTS
// GLOBAL VARIABLES
// Set display size in cols x rows (16x2 or 20x4)
const uint8_t COLS = 20;
const uint8_t ROWS = 4;
// INITIALIZE OBJECTS
// The init statement accepts the address and the number of columns and rows.
// All I2C components have an address, the default is usually 0x27
// If that doesn't work, see this: https://playground.arduino.cc/Main/I2cScanner/
LiquidCrystal_I2C lcd(0x27, COLS, ROWS);
/* LOCAL FUNCTIONS */
// This function clears a row by printing spaces.
void clearRow(int row){
const char* sixteen_spaces = " ";
const char* twenty_spaces = " ";
lcd.setCursor(0, row-1);
if(COLS == 16){
lcd.print(sixteen_spaces); //clears the row for a 16x2 display
}else if(COLS == 20){
lcd.print(twenty_spaces); //clears the row for a 20x4 display
}
lcd.setCursor(0, row-1); //cursor has to be set again after printing spaces
}
// Function to print to LCD on a single row.
// Takes the row number and the text to display on that row (max 16 or 20 chars, rest will be truncated).
// The entire display is cleared if the clearAll flag is true, else only the row is cleared (the default).
void printToLCDByRow(uint8_t row, String text, bool clear_all = false)
{
if(clear_all){
lcd.clear(); //clears the entire display
}else{
clearRow(row);
}
lcd.print(text);
}
// Function to print a long string to LCD across rows.
// Messages can be a maximum of 16x2(32) 20x4(80) chars, rest will be truncated.
// The entire display is cleared before printing the text.
void printToLcdAcrossRows(String text)
{
lcd.clear(); //clears the entire display
byte chars_remaining = text.length();
byte charFrom = 0;
byte charTo = chars_remaining < COLS ? text.length() : charFrom + COLS;
byte row = 1;
while(chars_remaining > 0){
String line = text.substring(charFrom, charTo);
printToLCDByRow(row, line);
chars_remaining = text.length() - charTo;
charFrom = charTo;
charTo = chars_remaining < COLS ? text.length() : charFrom + COLS;
row++;
}
}
// This function prints the text on a row and scrolls left if the length exceeds
// the number of columns.
void scrollTextLeft(int row, String text, bool clear_all = false) {
if(clear_all){
lcd.clear(); //clears the entire display
}else{
clearRow(row);
}
// Print as many characters as will fit on the display (number of cols)
// and if there are more characters then start scrolling.
Serial.println(text);
// For substring(), start index is 0 and end index should be one more than num chars required
// substring(0, 20) returns 20 chars, 0 to 19.
// Note that end index is the position not length.
printToLCDByRow(row, text.substring(0, COLS), false);
delay(2000);
if(text.length() > COLS){
uint8_t extra_chars = text.length() - COLS;
for (int i = 0; i < extra_chars; i++) {
// Redisplay the string starting from the second character until the last character is displayed.
printToLCDByRow(row, text.substring(i+1, i+1+COLS), false);
delay(2000); // Adjust delay for scrolling speed
}
}
}
void LCDSetup() // function to LCD setup
{
// Initializing wire
Wire.begin();
// Initialize the LCD.
lcd.init();
// Turn on the blacklight.
lcd.backlight();
// Clear the entire display.
lcd.clear();
// Use the function to display text on each line individually.
printToLCDByRow(1, "I2C LCD Ready!", true);
// This delay is to allow init to complete.
delay(1000);
if(OPERATING_MODE == 'D'){
Serial.println("The LCD is ready!");
}
}