#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define ROWS 4
#define COLS 4
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = { 2, 3, 4, 5 };
byte colPins[COLS] = { 6, 7, 8, 9 };
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address of the LCD
void setup() {
lcd.init();
lcd.backlight();
Serial.begin(9600);
}
void loop() {
char buffer[11];
int count = 0;
// Read the keypad
char key = getKey();
if (key != 0) {
if (key == '-') {
// Clear the buffer
for (int i = 0; i < 11; i++) {
buffer[i] = '\0';
}
count = 0;
lcd.clear();
} else if (key == '\n') {
// Display the buffer on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Artikelnummer:");
lcd.setCursor(0, 1);
lcd.print(buffer);
} else {
// Append the key to the buffer
if (count < 10) {
buffer[count++] = key;
lcd.print(key);
}
}
}
}
char getKey() {
// Scan the keypad
for (byte col = 0; col < COLS; col++) {
pinMode(colPins[col], OUTPUT);
digitalWrite(colPins[col], LOW);
for (byte row = 0; row < ROWS; row++) {
pinMode(rowPins[row], INPUT_PULLUP);
if (digitalRead(rowPins[row]) == LOW) {
delay(10); // debounce
while (digitalRead(rowPins[row]) == LOW); // wait for key release
return keys[row][col];
}
}
pinMode(colPins[col], INPUT);
digitalWrite(colPins[col], HIGH);
}
return 0;
}