Landing : Athabascau University
  • Blogs
  • Circuit 4A: LCD "Hello, World!"

Circuit 4A: LCD "Hello, World!"

I had no issues building the circuit. My program displays a message in two parts. The first part shows “Hello World!” and then 1.5 seconds later it is replaced with “My name is Adam!”. The whole message including both parts is 3 seconds long, and it is shown on the first line of the LCD. The second line displays the amount of time that has elapsed since the program started running in minutes, seconds, and milliseconds, as well as a count keeping track of the number of times the full message has been displayed. Here is the code:

#include <LiquidCrystal.h> //the liquid crystal library contains commands for printing to the display
LiquidCrystal lcd(7, 12, 11, 10, 9, 8); // tell the RedBoard what pins are connected to the display
String firstPart = "Hello World! "; //First part of message to be displayed
String secondPart = "My name is Adam!"; //Second part of messaage to be displayed
int messageTime = 3000; //Duration of entire message
int repetitions = 0; //Number of times the message has repeated
void setup() {
lcd.begin(16, 2); //tell the lcd library that we are using a display that is 16 characters wide and 2 characters high
lcd.clear(); //clear the display
}
void loop() {
lcd.setCursor(0, 0);
lcd.print(firstPart);
displayTime(messageTime/2);

lcd.setCursor(0, 0);
lcd.print(secondPart);
displayTime(messageTime/2);
repetitions++;
}
//This method prints the time for the specified duration
void displayTime(int maxDuration){
unsigned long startTime = millis();
unsigned long thisTime = 0;
unsigned int duration = 0;
while(duration < maxDuration){
thisTime = millis();
printTime(thisTime);
duration = thisTime-startTime;
}
}
//This method prints the specified time
void printTime(unsigned long thisTime){
unsigned int mins = thisTime/60000; //Divide milliseconds by 60000 to get minutes
unsigned int minutesRemainder = thisTime%60000; //Remainder of time less than one minute
unsigned int secs = minutesRemainder/1000; //Divide remainder by 1000 to get seconds
unsigned int ms = minutesRemainder%1000; //Remaining time in milliseconds
//These variables hold the number of digits in each int
unsigned int minsLength = String(mins).length();
unsigned int secsLength = String(secs).length();
unsigned int msLength = String(ms).length();
unsigned int repsLength = String(repetitions).length();
lcd.setCursor(0, 1);
//These if-statements print the time
if(mins < 100){
if(minsLength < 2){
lcd.print("0");
}
lcd.print(mins);
lcd.print(":");
if(secsLength < 2){
lcd.print("0");
}
lcd.print(secs);
lcd.print(":");
if(msLength < 3){
lcd.print("0");
if(msLength < 2){
lcd.print("0");
}
}
lcd.print(ms);
} else {
lcd.print(">99 mins ");
}
if(repetitions < 100){
lcd.print(" Rep:");
if(repsLength < 2){
lcd.print("0");
}
lcd.print(repetitions);
} else {
lcd.print(" Rep>99");
}

My program is based on the example program from the SIK Guide. I have added the displayTime() method that is responsible for displaying the elapsed time on the second line of the LCD. It is called after every time the message changes. It needs to be called multiple times because the message needs to be blinking while the elapsed time is updating on the LCD. The method takes an int parameter that specifies the duration that it is responsible for displaying the time. It calls the method printTime() to actually print each specific time on the display. printTime() takes an unsigned long parameter representing the amount of time in milliseconds that is to be displayed. The first few statements in printTime() translate the milliseconds into minutes, seconds, and milliseconds. The next few lines create variables to hold the number of digits required to represent the number of minutes, seconds, milliseconds, and repetitions. The last chunk of code is a bunch of if-else statements that determine how many leading zeroes need to be appended to each number and then print the numbers by calling lcd.print() several times.

Here is a video of the program in action:  LCD

The first thing I did while writing this program was to replace the message with a long string because I was curious about how the display would handle it. After running the program, I found that the string is truncated if it is more than 16 characters long. To avoid problems with fitting characters in the display I simply made sure that each part of the message took up exactly 16 characters. I did this by adding spaces to the end of the “Hello World!” string to make it exactly 16 characters, so the spaces overwrite any characters left over from the previous part of the message that was displayed. I used a timer to test the elapsed time shown on the display and found that it is pretty accurate, but I only tested for the first few minutes so hopefully it would still be accurate after running for hours.