// Pins connected to 74HC595
const int CLOCK_PIN = 27;
const int LATCH_PIN = 26;
const int DATA_PIN = 25;
// Column GPIOs
const int colPins[8] = {21, 19, 18, 17, 16, 4, 12, 14};
// Array to keep track of key states
bool keyStates[3][8] = {false};
void setRow(int row) {
// Offset by +1 because ROW 0 is connected to Q1 and not Q0
uint8_t rowValue = ~(1 << (row + 1));
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, rowValue);
// Latch to transfer the data to output pins
digitalWrite(LATCH_PIN, LOW);
digitalWrite(LATCH_PIN, HIGH);
// Small delay to allow the shift register output to stabilize
delayMicroseconds(10);
}
void setup() {
// Initialize serial for debugging
Serial.begin(115200);
// Initialize column pins
for (int i = 0; i < 8; i++) {
pinMode(colPins[i], INPUT_PULLUP);
}
// Initialize 74HC595 control pins
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(DATA_PIN, OUTPUT);
}
void loop() {
for (int row = 0; row < 3; row++) {
// Set the current row to LOW
setRow(row);
// Check each column for a key press
for (int col = 0; col < 8; col++) {
int currentState = digitalRead(colPins[col]);
if (currentState == LOW && !keyStates[row][col]) {
// Button was just pressed
keyStates[row][col] = true;
Serial.printf("COL %d, ROW %d: Pressed\n", col + 1, row + 1);
} else if (currentState == HIGH && keyStates[row][col]) {
// Button was just released
keyStates[row][col] = false;
Serial.printf("COL %d, ROW %d: Released\n", col + 1, row + 1);
}
}
}
delay(200); // Adjust as needed for scanning speed
}