Better LiquidCrystal_I2C library serialDisplay example.

Done something cool with one of our products? Got some helpful advice or a handy tutorial? Promote it here by posting or linking to your blog and tell the world.
Forum rules
LINKS TO AND FROM OTHER WEBSITES

Any links to third party websites located on this Website are provided for your convenience only. We have not reviewed each third party website and have no responsibility for such third party websites or their content. We do not endorse the third party websites or make representations about them or any material contained in them. If you choose to access a third party website linked to from this Website, it is at your own risk.

We reserve the right to delete any post linking to external websites which are deemed to either have no relevance to this forum, or where we believe the post was made for the intention of providing a financial benefit to the poster.
Post Reply
JamiePhonic
Posts: 2
Joined: Thu Sep 04, 2014 2:58 pm

Better LiquidCrystal_I2C library serialDisplay example.

Post by JamiePhonic » Tue Sep 09, 2014 1:56 pm

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
	}
}

Post Reply

Return to “Community Projects”