#define ROW_1 4
#define ROW_2 5
#define ROW_3 6
#define COL_1 15
#define COL_2 16
#define COL_3 17
#define COL_4 18
// Define row and column pin arrays
const int rowPins[3] = {ROW_1, ROW_2, ROW_3};
const int colPins[4] = {COL_1, COL_2, COL_3, COL_4};
// Store the previous state of each button in the matrix
bool previousButtonState[3][4] = {0};
void setup() {
Serial.begin(115200);
// Initialize row pins as outputs
for (int row = 0; row < 3; row++) {
pinMode(rowPins[row], OUTPUT);
digitalWrite(rowPins[row], HIGH); // Set row pins to high initially
}
// Initialize column pins as inputs with pull-up resistors
for (int col = 0; col < 4; col++) {
pinMode(colPins[col], INPUT_PULLUP);
}
}
void loop() {
// Scan the button matrix
for (int row = 0; row < 3; row++) {
// Set the current row to low
digitalWrite(rowPins[row], LOW);
// Read the state of each column
for (int col = 0; col < 4; col++) {
bool currentState = !digitalRead(colPins[col]); // Invert since LOW means pressed
if (currentState != previousButtonState[row][col]) {
if (currentState) {
Serial.print("Button pressed at row ");
Serial.print(row);
Serial.print(", column ");
Serial.println(col);
} else {
Serial.print("Button released at row ");
Serial.print(row);
Serial.print(", column ");
Serial.println(col);
}
previousButtonState[row][col] = currentState;
}
}
// Reset the current row to high
digitalWrite(rowPins[row], HIGH);
}
delay(20); // Adjust delay as needed
}