I'm using diodes on the columns.
Code: Select all
#include <Arduino.h>
const int numRows = 4;
const int numCols = 12;
const byte rowPins[numRows] = {1, 3, 17, 4};
const byte colPins[numCols] = {16, 0, 2, 15, 13, 12, 14, 27, 26, 25, 33, 32};
char keyMap[numRows][numCols] = {
{'\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '\b'},
{'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ';', '\'', '\n'},
{16, 'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '/', 16},
{' '}
};
void setup() {
Serial.begin(115200);
for (int i = 0; i < numRows; i++) {
pinMode(rowPins[i], OUTPUT);
digitalWrite(rowPins[i], LOW);
}
for (int j = 0; j < numCols; j++) {
pinMode(colPins[j], INPUT_PULLDOWN);
}
}
void loop() {
for (int row = 0; row < numRows; row++) {
digitalWrite(rowPins[row], LOW); // Activate the current row
for (int col = 0; col < numCols; col++) {
if (digitalRead(colPins[col]) == HIGH) {
char key = keyMap[row][col];
if (key == '\t') {
Serial.print("TAB");
} else if (key == '\b') {
Serial.print("BACKSPACE");
} else if (key == '\n') {
Serial.print("ENTER");
} else if (key == 16) {
Serial.print("SHIFT");
} else if (key != ' ') {
Serial.print(key); // Send the captured key to the serial console
}
delay(50); // Debounce delay
}
}
digitalWrite(rowPins[row], HIGH); // Deactivate the row
}
}