int rowPins[] = { 13, 12, 9, 8 };
int columnPins[] = { 7, 6, 5, 4 };
bool prevStates[16];
unsigned long pressTimes[16];
unsigned int delayTime = 20;
bool buttonsFiltered[16];
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(rowPins[i], OUTPUT);
pinMode(columnPins[i], INPUT_PULLUP);
}
Serial.begin(9600);
}
void loop() {
for (int r = 0; r < 4; r++) {
digitalWrite(rowPins[r], LOW);
for (int c = 0; c < 4; c++) {
bool buttonReading = !digitalRead(columnPins[c]);
int buttonIndex = r * 4 + c;
if (buttonReading != prevStates[buttonIndex]) {
pressTimes[buttonIndex] = millis();
}
if (millis() - pressTimes[buttonIndex] >= delayTime) {
if (buttonsFiltered[buttonIndex] != buttonReading) {
buttonsFiltered[buttonIndex] = buttonReading;
if (buttonsFiltered[buttonIndex]) {
Serial.print("Button ");
Serial.print(buttonIndex);
Serial.println(" has been pressed (filtered)");
} else {
Serial.print("Button ");
Serial.print(buttonIndex);
Serial.println(" has been depressed (filtered)");
}
}
}
prevStates[buttonIndex] = buttonReading;
}
digitalWrite(rowPins[r], HIGH);
}
}