/*
Filename: textSpecificLocation
Author: George Howard
Date: 26/03/2024
Description: This is a simple program to test writing to and LCD screen on the Arduino.
*/
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <util/delay.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int main (void) {
WriteLCDText (0, 0, "Hello");
_delay_ms(2500);
WriteLCDText (11, 0, "George Howard");
_delay_ms(2500);
WriteLCDText (18, 1, "Testing");
_delay_ms(2500);
WriteLCDText (0, 0, "Using all characters on display!");
}
void WriteLCDText(int col, int row, char *text) {
lcd.begin(16, 2);
// Check if col and row are within range
if (row < 0 || row > 1 || col < 0 || col > 15) {
lcd.setCursor(0, 0);
lcd.print("Out of range!");
return;
}
// Set the cursor to col and row
lcd.setCursor(col, row);
// Calculating
int remainingSpace = 16 - col;
int textLength = strlen(text);
// Reducing length of text if necessary
if (textLength > remainingSpace) {
textLength = remainingSpace;
}
// Displaying text
for (int i = 0; i < textLength; i++) {
lcd.print(text[i]);
}
if (strlen(text) > textLength) {
row += 1;
lcd.setCursor(0, row);
for (int i = textLength; i < strlen(text); i++) {
lcd.print(text[i]);
}
}
}