I am using Keypad library to build a musical MIDI keyboard using salvaged keyboard keys.
The matrix is 6 rows x 8 columns, with keymap as follow:
To test it, I used MultiKey.ino from keypad library examples.
/* @file MultiKey.ino
|| @version 1.0
|| @author Mark Stanley
|| @contact mstanley@technologist.com
||
|| @description
|| | The latest version, 3.0, of the keypad library supports up to 10
|| | active keys all being pressed at the same time. This sketch is an
|| | example of how you can get multiple key presses from a keypad or
|| | keyboard.
|| #
*/
#include <Keypad.h>
const byte ROWS = 6; //four rows
const byte COLS = 8; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','4','5','6','7','8'},
{'9','0','a','b','c','d','e','f'},
{'g','h','i','j','k','l','m','n'},
{'o','p','q','r','s','t','u','v'},
{'w','x','y','z','A','B','C','D'},
{'E','F','G','H','I','J','K','L'}
};
byte rowPins[ROWS] = {A3,A2,A1,A0,15,14}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {2,3,4,5,6,7,8,9}; //connect to the column pinouts of the kpd
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
unsigned long loopCount;
unsigned long startTime;
String msg;
void setup() {
Serial.begin(9600);
loopCount = 0;
startTime = millis();
msg = "";
}
void loop() {
loopCount++;
if ( (millis()-startTime)>5000 ) {
Serial.print("Average loops per second = ");
Serial.println(loopCount/5);
startTime = millis();
loopCount = 0;
}
// Fills kpd.key[ ] array with up-to 10 active keys.
// Returns true if there are ANY active keys.
if (kpd.getKeys())
{
for (int i=0; i<LIST_MAX; i++) // Scan the whole key list.
{
if ( kpd.key[i].stateChanged ) // Only find keys that have changed state.
{
switch (kpd.key[i].kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
case PRESSED:
msg = " PRESSED.";
break;
case HOLD:
msg = " HOLD.";
break;
case RELEASED:
msg = " RELEASED.";
break;
case IDLE:
msg = " IDLE.";
}
Serial.print("Key ");
Serial.print(kpd.key[i].kchar);
Serial.println(msg);
}
}
}
} // End looptype or paste code here
In general, all went well (incl. multi key press) except one thing:
- when I pressed 'E' and '2' simultaneously, 'F' is also unintentionally registered as 'pressed'.
- when I pressed 'F' and '3' simultaneously, '2' and 'G' is also unintentionally registered as 'pressed'.
- etc.. with the problem keys indicated in red color in above sketch.
Serial output is as follow:
Average loops per second = 130797
Average loops per second = 130783
Key 1 PRESSED.
Key 9 PRESSED.
Average loops per second = 130797
Key 1 HOLD.
Key 9 HOLD.
Key 1 RELEASED.
Key 1 IDLE.
Key 9 RELEASED.
Key 9 IDLE.
Key 2 PRESSED.
Key E PRESSED.
Key F PRESSED.
Key 2 HOLD.
Key E HOLD.
Key F HOLD.
Key 2 RELEASED.
Key E RELEASED.
Key F RELEASED.
Key 2 IDLE.
Key E IDLE.
Key F IDLE.
Average loops per second = 130802
Key F PRESSED.
Key 2 PRESSED.
Key 3 PRESSED.
Key G PRESSED.
Key F HOLD.
Key 2 HOLD.
Key 3 HOLD.
Key G HOLD.
Average loops per second = 130779
Key 2 RELEASED.
Key 3 RELEASED.
Key F RELEASED.
Key 2 IDLE.
Key 3 IDLE.
Key G RELEASED.
Key F IDLE.
Key G IDLE.
Average loops per second = 130796
Average loops per second = 130799
Each key on the keyboard has diode. I've checked the diodes, all seems normal.
Please help to overcome this problem. Thanks.