/*
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) {
WriteLCDInt (0, 0, 65536);
WriteLCDInt (14, 0, 123456);
WriteLCDInt (0, 0, -756);
}
void WriteLCDInt (int col, int row, long val) {
lcd.begin(16, 2);
// Check if col and row are within range
inRange(col, row, val);
// Setting the cursor to the user specified position
lcd.setCursor(col, row);
// Adjustments for negative integers
val = isNegative(val);
// Printing val
printDigits(col, row, val);
// 2.5 second delay for readibility
_delay_ms(2500);
}
// Function to make adjustments for negative values
long isNegative(long val) {
long value = val;
if (val < 0) {
value = val * -1;
lcd.print("-");
}
return value;
}
// Function to check if the column and row are valid
void inRange(int col, int row, long val) {
if (row < 0 || row > 1 || col < 0 || col > 15) {
lcd.setCursor(0, 0);
lcd.print("Out of range!");
return;
}
}
// Function to check how many digits are within a value
int intDigits(long value) {
int digits = 0;
if (value == 0) {
digits = 1;
}
while (value != 0) {
value /= 10;
digits++;
}
return digits;
}
// Function to return the value of an individual digit given a value and digit position
int individualDigit(long value, int position) {
int digit = 0;
for (int i = 0; i <= position; i++) {
digit = value % 10;
value /= 10;
}
return digit;
}
// Function to print the value of the user interger with text wrappin if necessary
void printDigits(int col, int row, long val) {
int remainingSpace = 16 - col;
int valueLength = intDigits(val);
if (valueLength > remainingSpace) {
for (int i = valueLength - 1; i >= valueLength - remainingSpace; i--) {
lcd.print(individualDigit(val, i));
}
row += 1;
lcd.setCursor(0, row);
for (int i = valueLength - remainingSpace - 1; i >= 0; i--) {
lcd.print(individualDigit(val, i));
}
} else {
for (int i = valueLength - 1; i >= 0; i--) {
lcd.print(individualDigit(val, i));
}
}
}