#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
int temp = 0;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
byte ZERO[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
B00000,
};
byte ONE[] = {
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000,
B10000
};
byte TWO[] = {
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
B11000,
};
byte THREE[] = {
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
B11100,
};
byte FOUR[] = {
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
B11110,
};
byte FIVE[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
uint8_t colPins[COLS] = { 5, 4, 3, 2 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.createChar(0, ZERO);
lcd.createChar(1, ONE);
lcd.createChar(2, TWO);
lcd.createChar(3, THREE);
lcd.createChar(4, FOUR);
lcd.createChar(5, FIVE);
}
void loop() {
int val = analogRead(A0);
int printVal = map(val, 0, 1023, 0, 80); // Map analog value to 0-80 range
int A= printVal / 5;
int B= printVal % 5;
lcd.setCursor(0, 0);
for (int i = 0; i < A; i++) {
lcd.write(5);
}
if (A < 16) {
lcd.write(B);
for (int i = A + 1; i < 16; i++) {
lcd.write(0);
}
// Display the key press if any
char key = keypad.getKey();
if (key != NO_KEY) {
Serial.println(key);
lcd.setCursor(1, 1); // Move to second row for key display
lcd.write(key);
}
}
}