Dear readers,
I am trying to make an MIDI keyboard out of an old keyboard. This keyboard works with a matrix of pushbuttons. So I combined the keypad library and the midi library in order to send a note on when a key is pressed and note off when a key is released. (arduino->midi->midi-usb converter->computer) The following code sort of works but the note off message is not always sent. When I check the keystate with another code, pressed, hold, released and idle all work fine. I am not an programming expert and I know the code could be a lot shorter, any help on this part is also welcome but is not my main problem (unless it is my bad programming that causes the problem).
Thanks in advance.
#include <Keypad.h>
#include <MIDI.h>
int velocity = 100;
int note = 60;
const byte ROWS = 5;
const byte COLS = 6;
//define the cymbols on the buttons of the keypads
char keys[ROWS][COLS] = {
{'Y','0','0','0','0','0'},
{'S','T','U','W','V','X'},
{'M','N','O','Q','P','R'},
{'G','H','I','K','J','L'},
{'A','B','C','E','D','F'}
};
byte rowPins[ROWS] = {12, 11, 10, 9, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
MIDI.begin(8);
}
void loop() {
char customKey = kpd.getKey();
// 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].kchar == 'A'){
note=60;
}
if (kpd.key[i].kchar == 'B'){
note=61;
}
if (kpd.key[i].kchar == 'C'){
note=62;
}
if (kpd.key[i].kchar == 'D'){
note=63;
}
if (kpd.key[i].kchar == 'E'){
note=64;
}
if (kpd.key[i].kchar == 'F'){
note=65;
}
if (kpd.key[i].kchar == 'G'){
note=66;
}
if (kpd.key[i].kchar == 'H'){
note=67;
}
if (kpd.key[i].kchar == 'I'){
note=68;
}
if (kpd.key[i].kchar == 'J'){
note=69;
}
if (kpd.key[i].kchar == 'K'){
note=70;
}
if (kpd.key[i].kchar == 'L'){
note=71;
}
if (kpd.key[i].kchar == 'M'){
note=72;
}
if (kpd.key[i].kchar == 'N'){
note=73;
}
if (kpd.key[i].kchar == 'O'){
note=74;
}
if (kpd.key[i].kchar == 'P'){
note=75;
}
if (kpd.key[i].kchar == 'Q'){
note=76;
}
if (kpd.key[i].kchar == 'R'){
note=77;
}
if (kpd.key[i].kchar == 'S'){
note=78;
}
if (kpd.key[i].kchar == 'T'){
note=79;
}
if (kpd.key[i].kchar == 'U'){
note=80;
}
if (kpd.key[i].kchar == 'V'){
note=81;
}
if (kpd.key[i].kchar == 'W'){
note=82;
}
if (kpd.key[i].kchar == 'X'){
note=83;
}
if (kpd.key[i].kchar == 'Y'){
note=84;
}
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:
MIDI.sendNoteOn(note, velocity, 8);//turn note on
break;
case RELEASED:
MIDI.sendNoteOn(note, 0, 8);//turn note on
}
}
}
}
}