Vielen Dank für die Antwort.
Ich habe gestern die MCP23017 Chips bekommen und meine Schaltung getestet, leider hängt sich der MCP23017 auf, wenn in einer Loop-Schleife ständig was geschrieben wird wie digitalWrite usw.
Zum Test habe ich vorher eine LED Blinken lassen, um Sicherzugehen, das alles funktioniert und war alles OK.
Nun habe ich meinen Sketch etwas umgeschrieben, um die Matrix auszulesen.
Hier ist das alte Sketch, diese ich noch mit Arduino Mega 2560 verwendet hatte und sehr gut funktioniert hat:
// Change here to the number of lines of your Master Layer
int masterLines = 4;
// Change here to the number of lines of your Slave Layer
int slaveLines = 16;
// Put here the pins you connected the lines of your Master Layer
int matrixMaster[] = {A0, A1, A2, A3};
// Put here the pins you connected the lines of your Slave Layer
int matrixSlave[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37};
void setup()
{
Serial.begin(9600);
for(int i = 0; i < slaveLines; i++){
pinMode(matrixSlave[i], INPUT_PULLUP);
}
for(int i = 0; i < masterLines; i++){
pinMode(matrixMaster[i], OUTPUT);
digitalWrite(matrixMaster[i], HIGH);
}
}
void loop()
{
String key = getKey();
if( key != 0) {
Serial.println(key);
}
}
String getKey()
{
String key = ""; // 0 indicates no key pressed
for(int i = 0; i < masterLines; i++){
digitalWrite(matrixMaster[i], LOW);
for(int j = 0; j < slaveLines; j++){
if(digitalRead(matrixSlave[j]) == LOW){ // Is a key pressed?
while(digitalRead(matrixSlave[j]) == LOW); // wait for key to be released
key = String(i)+","+String(j); // Remember which key
}
}
digitalWrite(matrixMaster[i], HIGH); // De-activate the current column.
}
return key;
}
Und hier ist das neue Sketch, diese ich mit ESP32 Dev Kit C V4 und 2 MCP23017 verwende:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
Adafruit_MCP23017 mcp1; // Create MCP 1
Adafruit_MCP23017 mcp2; // Create MCP 2
const uint8_t addr1 = 0; // Adresse 0x20 / 0
const uint8_t addr2 = 1; // Adresse 0x21 / 1
// Change here to the number of lines of your Master Layer
int masterLines = 4;
// Change here to the number of lines of your Slave Layer
int slaveLines = 16;
// Put here the pins you connected the lines of your Master Layer
int matrixMaster[] = {0, 1, 2, 3};
// Put here the pins you connected the lines of your Slave Layer
int matrixSlave[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
void setup() {
Serial.begin(9600);
mcp1.begin(addr1); // Start MCP 1
mcp2.begin(addr2); // Start MCP 2
mcp1.pinMode(0, OUTPUT);
mcp2.pinMode(0, OUTPUT);
for(int i = 0; i < slaveLines; i++){
mcp2.pinMode(matrixSlave[i],INPUT);
mcp2.pullUp(matrixSlave[i],HIGH);
}
for(int i = 0; i < masterLines; i++){
mcp1.pinMode(matrixMaster[i],INPUT);
mcp1.digitalWrite(matrixMaster[i], HIGH);
}
}
void loop() {
String key = getKey();
if( key != 0) {
Serial.println(key);
}
}
String getKey()
{
String key = ""; // 0 indicates no key pressed
for(int i = 0; i < masterLines; i++){
mcp1.digitalWrite(matrixMaster[i], LOW);
for(int j = 0; j < slaveLines; j++){
if(mcp2.digitalRead(matrixSlave[j]) == LOW){ // Is a key pressed?
while(mcp2.digitalRead(matrixSlave[j]) == LOW); // wait for key to be released
key = String(i)+","+String(j); // Remember which key
}
}
mcp1.digitalWrite(matrixMaster[i], HIGH); // De-activate the current column.
}
return key;
}
Wenn ich an meiner Matrix eine Taste Drücke, dann kommt erstmal nichts an, wenn ich ein Paar mal drücke, dann kommt es irgendwann an aber so, als würde der MCP23017 sehr stark hängen bleiben.
Wie könnte man die Funktion getKey() so umschreiben, das es mit den MCP23017 funktioniert?
Ich möchte gern ohne Delays arbeiten, da das Programm sonst für eine gewisse Zeit anhält und ich eigentlich noch viele andere Funktionen drin habe, die dann nicht mehr richtig funktionieren, wenn ich Delays einsetze.
Im Anhang habe ich ein Bild angehängt, wie ich die 2 MCP23017 verbunden habe.
Ich bedanke mich schon mal für die Hilfe.
Gruss