Formatting on a 20x4 LCD

Folks,

I'm working on code for a possible project for my sister in law. She likes to keep track of birthdays. So I wanted to build her a clock which displays a simple happy birthday message along with the name of the family member on the day of the persons birthday (from midnight to midnight). On days when there isn't a birthday, I want to display the temperature and humidity. However when testing the code I noticed trailing characters when switching between the happy birthday message and the temp and humidity due to the different lengths of the text. How would I go about dealing with this?

#include <Wire.h>                                 // include wire library
#include <RTClib.h>                               // RTC
#include <LiquidCrystal_I2C.h>                    // for LCD
#include "DHT_U.h"
#define Type DHT22

LiquidCrystal_I2C lcd(0x27, 20, 4);               // create LCD, address is 0x27
RTC_DS3231 rtc;                                   // create rtc, address is 0x68

DHT DHTT(sensePin, Type);
int sensePin=7;

float humidity;
float tempC;
float tempF;

// 0=Sunday, 1=Monday,...6=Saturday
const char dayInWords[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};    

// 0=" ", 1=Jan, 2=Feb, ... 12=Dec
const char monthInWords[13][4] = {" ", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", 
                                         "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
                                         
void setup()
{
  Serial.begin(9600);                             // start serial monitor for debugging if needed

  lcd.init();                                     // initialize LCD
  lcd.backlight();                                // turn on LCD backlight
    
  rtc.begin();                                    // initialize RTC

  DHTT.begin();

} //END of setup()


void loop()
{
  DateTime rtcTime = rtc.now();                   // get time

  int ss = rtcTime.second();                      // save seconds
  int mm = rtcTime.minute();                      // save minutes
  int hh = rtcTime.twelveHour();                  // etc.
  int DD = rtcTime.dayOfTheWeek();
  int dd = rtcTime.day();
  int MM = rtcTime.month();
  int yyyy = rtcTime.year();                      // save year

  lcd.setCursor(0,0);                             // Set cursor to top left position

  lcd.print(monthInWords[MM]);                    // print date in MMM-dd-yyyy format
  lcd.print("-");                                 // print "-" for formating
  if (dd<10) lcd.print ("0");                     // add 0 before hour if less than 10
  lcd.print(dd);                                  // print day
  lcd.print("-");                                 // print "-" for formatting
  lcd.print(yyyy);                                // print year
  lcd.print("  ");                                // print space for formatting
  lcd.print(dayInWords[DD]);                       // print day in DDD format
  
  lcd.setCursor(0,1);                             // set cursor at beginning of next row

  if (hh<10) lcd.print("0");                      // add 0 before hour if less than 10
  lcd.print(hh);                                  // print hour
  lcd.print(":");                                 // add ":" for formatting
  if (mm<10) lcd.print("0");                      // add 0 before minite if less than 10
  lcd.print(mm);                                  // print minutes
  lcd.print(":");                                 // add ":" for formatting
  if (ss<10) lcd.print("0");                      // add 0 before seconds if less than 10
  lcd.print(ss);                                  // print seconds
  if (rtcTime.isPM()) lcd.print(" PM");           // print PM if PM
  else lcd.print (" AM");                         // else print AM
  
  humidity = DHTT.readHumidity();
  tempC = DHTT.readTemperature();
  tempF = DHTT.readTemperature(true);

  if (MM == 1 && dd == 6 && hh == 7 && mm == 16) 
  {
    lcd.setCursor(0,2);
    lcd.print("Happy Birthday");
    lcd.setCursor(0,3);
    lcd.print("Harry Wagner");
  }
  else {
    lcd.setCursor(0,2);
    lcd.print("Temperature: ");
    lcd.print(tempF);
    lcd.setCursor(0,3);
    lcd.print("Humidity: ");
    lcd.print(humidity);
  }
  

}

Best,

Tony

The easiest way,

clear the LCD screen before writing a new message

There should be a function called lcd.clear() or something. Take note though, this will literally empty everything on the screen and your cursor will start at 0,0

It flickers horribly when I use lcd.clear(). I think I'm clearing and writing constantly. I was hoping there was a relatively simple strategy but I don't see it.

That is correct. Does the information that needs to be displayed change that fast? You can slow it down by adding delay at the end

Clear the display when going between the birthday message and the temperature/humidity message.
Once you are on the temperature/humidity message, only update the data itself, and then only when it changes. No need to clear the display for that, just overwrite the previous data.

Some hints on using the display.

Never use clear() in loop() code.

There is no reason to update a display more often than about 2 times a second. Use millis timing to do updates, not delay().
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Only update the display when the data changes.

Write constant length data to the display. For instance with a 20 character display write 20 characters even if you have to pad with spaces.

lcd.setcursor(0,0);
lcd.print("Happy B'day         ");  // overwrite old data with spaces

Or
Clear the space that you will write to with spaces, use setCursor to reset the cursor and write your data.

lcd.setcursor(0,0);
lcd.print("                    ");  // overwrite old data with spaces
lcd.setcursor(0,0);
lcd.print("Happy B'day");

Thanks all. Karma sent.

@groundFungus I've started started to learn about millis(). Hopefully it will be a short learning curve, but I doubt it with me. Thanks for the links.

@david_2018 That sounds interesting. I'm still new to coding so I might not be able to pull it off but I'll look into it.

@hzrnbgy Thank you.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.