Hay guys!
So i've been trying to find some code that will read in data from a serial connection and split that data into 4, 20 character "blocks" which will then print those "blocks" to a 20x4 I2C controlled LCD panel.
Essentially, i need a function to properly wrap the text on an lcd
so far, i have the last part of the code down and i know there is probably a much better way to do this, but im new to arduino, so i have no idea where to go from this.
char* Data[] = { "This is block 1", "This is block 2", "This is block 3", "This is block 4" };
for (int i = 0; i < 4; i++)
{
lcd.setCursor(0, i);
lcd.print(Data[i]);
}
The reason i need a function for this is that the screens buffer is set up in that it will write line 1, then line 3, then line 2, then line 4 when you send more than 20 characters
i found this thread which comes pretty close to what i need, but the function takes no inputs and causes compile errors
UPDATE 1:
I managed to butcher together some code from various other forum posts and got it sort of working. heres the program as it stands:
/*
DEVICE PINOUT (SPI Interface):
PIN 1: GND
PIN 2: +5V
PIN 3: SDA - Connect to Arduino analogue PIN 4
PIN 4: SCL - Connect to Arduino analogue PIN 5
*/
//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 20x4 line display */
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int LEDPin = 13;
char* Data[] = { "This is string 01", "This is string 02", "This is string 03", "This is string 04" };
char inData[80]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup()
{
Serial.begin(9600);
Serial.println("Serial started");
pinMode(LEDPin, OUTPUT);
/* Initialise the LCD */
Serial.println("Attempting to Initalise LCD (1/2)");
lcd.init();
Serial.println("Attempting to Initalise LCD (2/2)");
lcd.init();
Serial.println("LCD Initalised");
Serial.println("Enabled backlight");
lcd.backlight();
for (int i = 0; i < 4; i++)
{
lcd.setCursor(0, i);
lcd.print(Data[i]);
}
}
/* Main program loop */
void loop()
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// read all the available characters
while (Serial.available() > 0) {
if (index < 79) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
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); // 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.println("echoing string:");
Serial.println(stringIn);
Serial.println("");
while (stillProcessing) {
if (++lineCount > 20) { // have we printed 20 characters yet (+1 for the logic)
lineNumber++;
lcd.setCursor(0, lineNumber); // move cursor down
lineCount = 1;
}
lcd.print(stringIn[charCount - 1]);
if (!stringIn[charCount]) { // no more chars to process?
stillProcessing = 0;
}
charCount++;
}
}