So Im currently working on multiple 4x4 Button Matrices which I want to read as fast as possible/ reasonable. My current Code isn't really working well cause its only able to read the entire Matrix around 5-7 Times a second, only getting slower the more matrices I add.
The below posted Code is the simplified form of the current Sketch Im using, but the part that causes the problem is definitely in it. It reads 4 Button Matrices and stores their Values in a 2D Array called 'MatrixValues'. As said, the code isn't really well made; before I nicely parted everything in reusable functions but that made it even slower.
#include <Adafruit_MCP23X17.h>
Adafruit_MCP23X17 mcp0;
Adafruit_MCP23X17 mcp1;
Adafruit_MCP23X17 mcp2;
//Matrix PCBs
const byte MatrixColumn = 4;
const byte MatrixRow = 4;
const byte MatrixColumnPins[6][MatrixColumn] = { {8, 9, 10, 11}, {0, 1, 2, 3}, {8, 9, 10, 11}, {0, 1, 2, 3}, {8, 9, 10, 11}, {0, 1, 2, 3} };
const byte MatrixRowPins[6][MatrixRow] = { {12, 13, 14, 15}, {4, 5, 6, 7}, {12, 13, 14, 15}, {4, 5, 6, 7}, {12, 13, 14, 15}, {4, 5, 6, 7} };
const byte totalMatrixKeys = MatrixColumn*MatrixRow;
int MatrixValues[6][totalMatrixKeys];
int MatrixValuesOld[6][totalMatrixKeys];
void setup() {
Serial.begin(115200);
mcp[0].begin_I2C(0x20);
mcp[1].begin_I2C(0x21);
mcp[2].begin_I2C(0x22);
//Matrix PCBs
for (byte i = 0; i < MatrixColumn; i++) {
mcp[0].pinMode(MatrixColumnPins[0][i], INPUT);
mcp[1].pinMode(MatrixColumnPins[1][i], INPUT);
mcp[2].pinMode(MatrixColumnPins[2][i], INPUT);
}
}
void loop() {
readExecuter();
sendExecuter();
Serial.println("Round"); //used for measuring 'round-trip-time'
}
void readExecuter() {
int keyNumber = 0;
// Rows...
for (byte row = 0; row < 4; row++) {
mcp[0].pinMode(MatrixRowPins[0][row], OUTPUT); //--> it reads all pin definitions from the global 2D Array MatrixRowPins
mcp[0].pinMode(MatrixRowPins[1][row], OUTPUT);
mcp[1].pinMode(MatrixRowPins[2][row], OUTPUT);
mcp[1].pinMode(MatrixRowPins[3][row], OUTPUT);
mcp[0].digitalWrite(MatrixRowPins[0][row], LOW);
mcp[0].digitalWrite(MatrixRowPins[1][row], LOW);
mcp[1].digitalWrite(MatrixRowPins[2][row], LOW);
mcp[1].digitalWrite(MatrixRowPins[3][row], LOW);
//Columns...
for (byte column = 0; column < 4; column++) {
int keyState0 = mcp[0].digitalRead(MatrixColumnPins[0][column]); //--> it reads all pin definitions from the global 2D Array MatrixColumnPins
int keyState1 = mcp[0].digitalRead(MatrixColumnPins[1][column]);
int keyState2 = mcp[1].digitalRead(MatrixColumnPins[2][column]);
int keyState3 = mcp[1].digitalRead(MatrixColumnPins[3][column]);
MatrixValues[0][keyNumber] = keyState0;
MatrixValues[1][keyNumber] = keyState1;
MatrixValues[2][keyNumber] = keyState2;
MatrixValues[3][keyNumber] = keyState3;
keyNumber++;
}
mcp[0].pinMode(MatrixRowPins[0][row], INPUT);
mcp[0].pinMode(MatrixRowPins[1][row], INPUT);
mcp[1].pinMode(MatrixRowPins[2][row], INPUT);
mcp[1].pinMode(MatrixRowPins[3][row], INPUT);
}
}
void sendExecuter() {
for (int p = 0; p < 4; p++) {
for (int i = 0; i < 16; i++) {
if (MatrixValues[p][i] != 1 && MatrixValues[p][i] != MatrixValuesOld[p][i]) {
Serial.print("Matrix: ");
Serial.print(p);
Serial.print(" | ");
Serial.print("Key: ");
Serial.println(i);}
MatrixValuesOld[p][i] = MatrixValues[p][i];}}
}
Summing the individual Matrices in one big by hardware isn't an option; it has to work with individual ones. I also tried using the pre made librarys for Matrices with the MCP expanders but that did not work at all for me -> literally no output no matter what I've tried,
Help and advice is really appreciated. Thank you!



