I'm trying to program a mechanical keyboard running on the Arduino Pro Micro (5V/16MHz). I'm using a matrix approach (4x12), and while I believe the code and the wiring (both of which I've checked multiple times) are correct, I can't get the first two rows to work no matter what. I've tried wiring it up to other pins, which works, but only for two rows at a time, when I hook up all the 4 rows, the two ones on top don't work. The code:
/*cols and rows*/
byte rows[] = {2,3,4,5};
const int row_Count = 4;
byte cols[] = {6,7,8,9,10,14,15,16,18,19,20,21};
const int col_Count = 12;
byte keyboard[col_Count][row_Count];
void read_layout() {
/*do the columns*/
for (int col_Index=0; col_Index < col_Count; col_Index++) {
/*setup for registering the column state*/
byte cur_Col = cols[col_Index];
pinMode(cur_Col, OUTPUT);
digitalWrite(cur_Col, LOW);
/*do the rows*/
for (int row_Index=0; row_Index < row_Count; row_Index++) {
byte row_Col = rows[row_Index];
pinMode(row_Col, INPUT_PULLUP);
keyboard[col_Index][row_Index] = digitalRead(row_Col);
pinMode(row_Col, INPUT);
}
/*disable the column*/
pinMode(cur_Col, INPUT);
}
}
void print_layout() {
for (int row_Index=0; row_Index < row_Count; row_Index++) {
Serial.print(row_Index);
Serial.print(F(": "));
for (int col_Index=0; col_Index < col_Count; col_Index++) {
Serial.print(keyboard[col_Index][row_Index]);
if (col_Index < col_Count) Serial.print(F(", "));
}
Serial.println("");
}
Serial.println("");
}
void setup() {
Serial.begin(115200);
delay(2000);
/*init the rows*/
for(int i=0; i<row_Count; i++) {
pinMode(rows[i], INPUT);
}
/*init the cols*/
for (int i=0; i<col_Count; i++) {
pinMode(cols[i], INPUT_PULLUP);
}
}
void loop() {
read_layout();
print_layout();
}
two ones on top
is meaningless ... how are they connected?INPUT_PULLUP
? -- Unrelated: the condition inprint_layout()
does not prevent the printing of the last comma.col_Index
is always lower thancol_Count
there. -- Unrelated: You can automate the calculation of the sizes, for exampleconst int col_Count = sizeof cols / sizeof cols[0];
.