I am working on a project involving a 16x2 LCD screen that uses an i2c driver. I am struggling to get the output to be displayed on the second row once the first row is fully occupied. When the first row is full, any output just does not get printed, however, it appears in the serial monitor. I am not very familiar with the liquidcrystal_i2c library
Basically what the project does is convert morse code into text. I used https://github.com/DeviNoles/Morse_Code_Youtube/blob/main/morsebuzz.ino for the code and made some minor changes like removing the buzzer.
Any ideas?
The code:
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int led = 8;
int buttonState = 0;
bool lastButtonState = false;
int startPressed = 0;
int endPressed = 0;
int holdTime = 0;
String buff = "";
void setup() {
lcd.begin();
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
buttonState = digitalRead(led);
if (buttonState == LOW) {
if ((millis() - endPressed) % 65536 > 500) {
if (buff != "") {
Serial.println(retBuf());
clearBuf();
lastButtonState = true;
}
}
if ((millis() - endPressed) % 65536 > 1500) {
if (lastButtonState == true) {
Serial.println("SPACE HERE");
lcd.print(" "); // Print a space on the LCD
lastButtonState = false;
}
}
} else if (buttonState == HIGH) {
startPressed = millis();
updateState();
}
}
void updateState() {
while (digitalRead(led) == HIGH) {
endPressed = millis();
}
holdTime = endPressed - startPressed;
Serial.println(holdTime);
if (holdTime < 500) {
Serial.println("DIT");
buff = buff + ".";
} else if (holdTime >= 500) {
Serial.println("DAH");
buff = buff + "-";
}
}
String retBuf() {
return morseToChar(buff);
}
String morseToChar(const String &morse) {
static String characters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."};
for (int k = 0; k < sizeof(characters) / sizeof(characters[0]); k++) {
if (characters[k] == morse) {
char decodedChar;
if (k < 26) {
decodedChar = (k + 'A');
} else if (k < 36) {
decodedChar = (k - 26 + '0');
} else {
return "ERROR"; // Morse code does not represent symbols or special characters in this example.
}
lcd.print(decodedChar);
return String(decodedChar);
}
}
return "ERROR";
}
void clearBuf() {
buff = "";
}
