When I press '#' on the keypad in this program, it rarely (but sometime does) puts the requested "CLEARED SCREEN" to the LCD. What is wrong here? It is giving me a headache.
It ends up corrupting the text on LCD after a few keypresses.
As this is just the start of a bigger program, in this state, I just expect it to show the typed numbers/letters to screen and the '#' key to show "CLEARED SCREEN". It does type the inputs mostly, but can corrupt screen after a few key presses.
#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);
lcd.setCursor(0, 3);
lcd.print(str);
Serial.println(sizeof(str));
Serial.println(str);
d = d + 1;
}
}
}