/*
https://forum.arduino.cc/t/lcd-text-being-corrupted-by-something/1261627
*/
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
char str[17] = " ";
byte rowPins[ROWS] = { 5, 4, 3, 2 }; //connect to the row pinouts of the keypad
byte colPins[COLS] = { 9, 8, 7, 6 }; //connect to the column pinouts of the keypad
int d = 0;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("PLAY SONG NUMBER....");
lcd.setCursor(0, 1);
lcd.print("");
lcd.setCursor(0, 2);
lcd.print("");
lcd.setCursor(0, 3);
lcd.print("");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == '#') {
strcpy(str, " ");
lcd.setCursor(0, 3);
lcd.print("CLEARED SCREEN");
delay(50);
d = 0;
str[d + 1] = '\0';
}
if (key != '#' and d < 16) {
Serial.println(key);
str[d] = key;
str[d + 1] = '\0';
Serial.println(d);
if (d == 0) {
lcd.setCursor(0, 3);
lcd.print(" ");
}
lcd.setCursor(0, 3);
lcd.print(str);
Serial.print("will always be:");
Serial.println(sizeof(str));
Serial.println(str);
Serial.print("Length of string :");
Serial.println(strlen(str));
d = d + 1;
}
}
}