#include <LiquidCrystal.h>
// Pin definitions
#define ROW_PORT PORTF
#define COL_PIN PINK
#define ROW_DDR DDRF
#define COL_DDR DDRK
LiquidCrystal lcd(8, 9, 10, 11, 12, 13); // Adjust LCD pins if needed
void init_program() {
ROW_DDR = 0x0F; // Rows as outputs (lower 4 bits)
COL_DDR = 0x00; // Columns as inputs (lower 4 bits)
ROW_PORT = 0x0F; // Drive rows HIGH
}
void switch_in(uint8_t rowMask) {
ROW_PORT = rowMask; // Activate only the specified row
delay(5); // Allow signals to stabilize
}
uint8_t switch_out() {
return COL_PIN & 0x0F; // Read only the lower 4 bits (columns)
}
char get_key(uint8_t row, uint8_t col) {
const char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
return keys[row][col];
}
void setup() {
init_program();
lcd.begin(16, 2);
lcd.print("Press a key");
Serial.begin(9600);
}
void loop() {
for (int row = 0; row < 4; row++) {
switch_in(1 << row); // Activate one row at a time
uint8_t colValue = switch_out(); // Read column values
for (int col = 0; col < 4; col++) {
if (colValue & (1 << col)) { // Check if column is active
char key = get_key(row, col);
lcd.clear();
lcd.print("Key: ");
lcd.print(key);
Serial.println(key);
delay(500); // Debounce delay
}
}
}
}