Most systems produce messages that give the user feedback and error messages. A user needs to see what happened in the past - for example, if a system fell over this afternoon, it's essential to see the messages from this morning, to work out what happened.
The 16 x 2 LCD screen allows 2 messages to appear at once, one on each line. This is nowhere near enough to work out
where an error may have occurred.
This system stores 10 messages, and allows the user to scroll up and down the list using a switch. It can easily be extended
to store and display as many messages as you wish.
This code I developed myself, and I put it into the public domain. If you find it useful, I'd appreciate a feedback message.
// Display up to 10 messages on a 16x2 LCD
// Author : Paul Goodliffe
// Contact : paulgoodliffe@ymail.com
// Date : May 2014
// Licence : Public domain freeware.
//
// Connect up the LCD for usual display purposes
// Connect a switch to arduino pin 7 to scroll upwards thru the messages
// Connect a switch to arduino pin 6 to scroll down thru the messages
// Call the function addMessage() to write a message. Up to 10 are stored.
//
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5,4,3,2);
int msgUpPin = 7; // The switch to scroll messages UP is on pin 7
int msgUpBtn = 0; // the value that is read from that switch
int msgDownPin = 6; // The switch to scroll messages DOWN is on pin 6
int msgDownBtn = 0; // the value that is read from that switch
int numMsgsInArray = 0; // This has a maximum of 10 messages
String messages[10]; // because we've defined 10 Strings
int msg_num = 1; // This is between 1 and 10, reflecting which message number is printed on the LCD
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(msgUpPin, INPUT);
pinMode(msgDownPin, INPUT);
// Add a couple of sample messages, so that we see something.
addMessage("first message Hello");
addMessage("second message World");
addMessage("the third msg");
Serial.begin(9600); // Only needed if you want to view the messages on your monitor as well
printMessages(); // start off by printing the first message on the LCD
}
void loop() {
delay(40); // Slow the machine down to a sensible state, run eg. 25 times per sec
checkUpButton();
checkDownButton();
//
// Put the rest of your code here, using function addMessage() to send a message when you wish.
//
}
void checkDownButton()
{
msgDownBtn = digitalRead(msgDownPin);
if (msgDownBtn == HIGH)
{
msg_num--;
if (msg_num < 1)
{
msg_num = 1;
}
printMessages();
}
}
void checkUpButton()
{
msgUpBtn = digitalRead(msgUpPin);
if (msgUpBtn == HIGH)
{
msg_num++;
if (msg_num > numMsgsInArray)
{
msg_num = numMsgsInArray;
}
printMessages();
}
}
void addMessage(char *newMsg)
{
numMsgsInArray++;
if (numMsgsInArray >= 11)
{ // To shift the messages up in the array,
// get the length of the string and reserve it,
// Then shift the msg to the reserved space.
// Note that I have used "human values" so that "number of messages" is correct (between 1 and 10), and
// the code subtracts 1, to point to the correct array entry (between 0 and 9).
for (int i = 1; i <= 9; i++)
{
int tmp1Length = (messages[i].length());
messages[i-1].reserve(tmp1Length);
messages[i-1] = messages[i];
}
numMsgsInArray = 10;
}
int strLength = strlen(newMsg)+1; //Include space for \0
messages[numMsgsInArray-1].reserve(strLength);
messages[numMsgsInArray-1] = newMsg;
}
void printMessages()
{
// If you want to see the messages on your serial monitor (eg for testing the code)
Serial.println("Now it is");
for (int i = 1; i <= numMsgsInArray; i++)
{
Serial.println(messages[i-1]);
}
//
// Now, print the messages out to the LCD screen.
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(messages[msg_num-1]);
// Print the next message on the following line, if it exists
lcd.setCursor(0, 1);
if (msg_num < numMsgsInArray)
{
lcd.print(messages[msg_num]);
}
delay(300); // Give the user time to lift their finger off the button
}
|
| Wired up and working. |
|
The wiring:
The LCD is connected as before, see here.
Violet and Blue are connected to pins 6 and 7, the switches. The default state is a drop down
resistor to ground. The 'switch' in this case is the spare red cable, connected via a resistor to +5V. In this
testing phase, I simply
touch the spare cable to the drop down resistor to simulate a switch push.