#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Pin baris keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Pin kolom keypad// connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputString = ""; // String to store entered characters
const int maxInput = 5; // Maximum input length
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Enter Input:");
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key == 'C') {
// Clear the input string
inputString = "";
clearLCD("Input Cleared");
}
else if (key == 'D') {
// Delete the last character
if (inputString.length() > 0) {
inputString.remove(inputString.length() - 1);
}
}
else {
// Add the key to the string if it's not full
if (inputString.length() < maxInput) {
inputString += key;
} else {
displayErrorMessage("Max Input");
}
}
printToLCD();
}
}
void printToLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(inputString);
}
void displayErrorMessage(const char *message) {
lcd.setCursor(0, 1);
lcd.print("Error: ");
lcd.print(message);
delay(2000); // Display the error message for 2 seconds
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the error message
}
void clearLCD(const char *message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
delay(2000); // Display the clear message for 2 seconds
lcd.clear();
}