Page 1 of 1

Better LiquidCrystal_I2C library serialDisplay example.

Posted: Tue Sep 09, 2014 1:56 pm
by JamiePhonic
This sketch takes the "serialDisplay" example included with the LiquidCrystal_I2C library, and adds a crude text wrapping function to it.

It's compatable with both the 1602 and 2004 displays. Just change the 2 variables 'Rows' and 'Chars' at the top to match the display you have.

Code: Select all

/*
DEVICE PINOUT (SPI Interface):

PIN 1: GND
PIN 2: +5V
PIN 3: SDA - Connect to Arduino analogue PIN 4 (or the dedicated SDA pin on the arduino uno R3)
PIN 4: SCL - Connect to Arduino analogue PIN 5 (or the dedicated SCL pin on the arduino uno R3)
*/


const int Rows = 4; // How many lines does your LCD have?
const int Chars = 20; // How may characters can be displayed per line?


//Include the SPI/IIC Library
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

/* Initialise the LiquidCrystal library. The default address is 0x27 and this is a 20 character, 4 line display (as set by the Rows and Chars variables above) */
LiquidCrystal_I2C lcd(0x27, Chars, Rows); // Create the LCD object

char inData[(Rows*Chars)]; // Allocate some space for the incoming serial string, capped at Rows x Chars (i.e. 20x4, or 80 chars max) to prevent overflow
char inChar; // Where to store each character we read over serial before adding it to 'inData'
byte index = 0; // The current index in the 'inData' array where the character in 'inChar' will be added


void setup()
{
	Serial.begin(9600); // Open a serial connection
	delay(100); // Wait 100 milli seconds
	Serial.println("Serial started"); // Print "serial started" to the console then move to a new line
	/* Initialise the LCD */
	Serial.println("Attempting to Initalise LCD..."); // Print whats happening to the serial monitor
	lcd.init(); // Call the special I2C LCD initalisation function
	Serial.println("LCD Initalised!"); // Update the serial monitor
	lcd.backlight(); // Turn on the LCD backlight

	lcd.print("Welcome!"); 
	lcd.setCursor(0, 1);
	lcd.print("Send something over");
	lcd.setCursor(0, 2);
	lcd.print("serial to see it");
	lcd.setCursor(0, 3);
	lcd.print("here");
	delay(1000);
	Serial.println("Ready to accept serial data");
}

/* Main program loop */
void loop()
{
	if (Serial.available()) // when characters arrive over the serial port...
	{
		delay(100); // wait a bit for the entire message to arrive
		
		while (Serial.available() > 0) // read all the available characters
		{
			if (index < 79) // If the value of 'index' is still one less than the size of the 'inData' array
			{
				inChar = Serial.read(); // Read a character
				inData[index] = inChar; // Store it in 'inChar'
				index++; // Increment where to write next
				inData[index] = '\0'; // Add a Null character to terminate the string
			}
		}
		Serial.flush(); // Flush the serial buffer
		stringToLCD(inData); // call the 'stringToLCD function' and pass it 'inData' to format and send the data we recieved to the LCD
		index = 0; // Set the index back to 0 ready for the next stream of data
	}
}

void stringToLCD(char stringIn[]) {
	int lineCount = 0;
	int lineNumber = 0;
	byte stillProcessing = 1;
	byte charCount = 1;
	lcd.clear();
	lcd.setCursor(0, 0);
	Serial.println("");
	Serial.print("Printing: '"); // Echo back to serial what's going to be shown on the LCD
	Serial.print(stringIn);
	Serial.println("' to display");

	while (stillProcessing) // While there are still characters to print
	{
		if (++lineCount > Chars) {    // have we printed 20 characters yet?
			lineNumber++; // If so, move the cursor to the next line
			lcd.setCursor(0, lineNumber);   // move cursor down
			lineCount = 1;
		}

		lcd.print(stringIn[charCount - 1]); // Print the current character at the index of 'charCount' from the 'stringIn' array (which contains the data from 'inData')

		if (!stringIn[charCount])  // no more chars to process?
		{  
			stillProcessing = 0; // Set 'stillProcessing' to 0 to exit the while loop
		}
		charCount++; // Add 1 to the charCount to process the next character in the array
	}
}