Using a Mega and serial example to drive a 16x2 lcd and it won't show more than one line when the alphabet is typed. (a to p only)
Anyone else seen this?
You need to specifically program it to display on line 2.
Post your code.
/*
LiquidCrystal Library - Serial Input
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch displays text sent over the serial port
(e.g. from the Serial Monitor) on an attached LCD.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystalSerial
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}
void loop() {
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}
That looks like some basic display code.
To write to the bottom row u need to set the cursor to position 0 on row 1.
lcd.setCursor(0,1);
To wrap the text I would :
- read (the first 32 bytes/characters) into a buffer
- set the cursor at (0,0)
- write the first 16 characters to the lcd
- set the cursor at (0,1)
- write the rest of the buffer to the lcd
- have a short delay
- repeat above for longer text in excess of 32 bytes/characters
I suppose you could even write a function to do the above.
FWIW, when reading serial, the items read will be cleared from Serial.available().
I am not sure if there is a library available that might have a function that does text wrapping. I imagine it might well exist. Someone else may know.
Here's crude start that scrolls a long text message on the 2nd line of a 16x2 display. I'm using the Yourduino I2C display, so you will likely need to change the liquid crystal constructor call and the include file. It may, however, get you started.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DISPLAYCOL 16
#define DISPLAYROW 2
#define VISUALDELAY 200
#define SCROLLLINE 1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup() {
//char testMsg[] = "It doesn't inter"; // A 16 char test string
char testMsg[] = "It doesn't interest me who you know or how you came to be here. I want to know if you will stand in the centre of the fire with me and not shrink back.";
int length;
lcd.begin(DISPLAYCOL, DISPLAYROW); // Display LCD
lcd.clear();
length = strlen(testMsg);
DisplayText(testMsg, length);
}
void loop() {
}
void DisplayText(char *ptr, int length)
{
char buffer[DISPLAYCOL + 1];
char *displayPtr;
int counter = 0;
lcd.setCursor(0, SCROLLLINE);
if (length <= DISPLAYCOL) { // If it all fits on one line...
lcd.print(ptr); // ...show it and...
return; // ...go home
}
displayPtr = ptr + DISPLAYCOL; // Otherwise break it and show it...
while ( (displayPtr - ptr) < (length - DISPLAYCOL) ) {
displayPtr++;
delay(VISUALDELAY);
memmove(buffer, displayPtr, DISPLAYCOL);
lcd.setCursor(0, SCROLLLINE);
lcd.print(buffer);
}
}