#include <Arduino.h>
const uint8_t ROWS = 3;
const uint8_t COLS = 3;
char keys[ROWS][COLS] = {
{ '1', '2', '3' },
{ '4', '5', '6' },
{ '7', '8', '9' }
};
uint8_t colPins[COLS] = { A3, A4, A5 };
uint8_t rowPins[ROWS] = { A0, A1, A2 };
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set column pins as outputs and row pins as inputs
for (uint8_t col = 0; col < COLS; col++) {
pinMode(colPins[col], OUTPUT);
digitalWrite(colPins[col], HIGH); // Set column pin high
}
for (uint8_t row = 0; row < ROWS; row++) {
pinMode(rowPins[row], INPUT_PULLUP); // Use internal pull-up resistors
}
}
void loop() {
// Scan the keypad matrix
for (uint8_t col = 0; col < COLS; col++) {
digitalWrite(colPins[col], LOW); // Set current column low
for (uint8_t row = 0; row < ROWS; row++) {
if (digitalRead(rowPins[row]) == LOW) {
// Key press detected
char key = keys[row][col];
Serial.println("Key pressed: " + String(key));
// Wait until the key is released
while (digitalRead(rowPins[row]) == LOW) {
// Wait
}
}
}
digitalWrite(colPins[col], HIGH); // Restore current column to high
}
}