I’m using Arduino’s Keypad.h library, with a piano circuit board key matrix, every key works, even multiple keypresses at once. However any double digit, so for example “10” results in “0”, or 12 results in “2”.
I will attach a snippet of my code
/* @file MultiKey.ino
|| @version 1.0
|| @author Mark Stanley
|| @contact [email protected]
||
|| @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>
int velocity = 100;//velocity of MIDI notes, must be between 0 and 127
//higher velocity usually makes MIDI instruments louder
int noteON = 144;//144 = 10010000 in binary, note on command
int noteOFF = 128;//128 = 10000000 in binary, note off command
const byte ROWS = 6; //four rows
const byte COLS = 6; //three columns
const uint8_t keys[4][6] = {
{'2','1','3','4','5','6'},
{'6','5','7','8','9','11'},
{'13','12','14','15','16','17'},
{'19','18','20','21','22','23'}
};
byte rowPins[COLS] = {23, 25, 27, 29, 31, 33}; //connect to the row pinouts of the kpd
byte colPins[ROWS] = {A8, A9, A10, A11}; //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 = 144;
Serial.write(144);//send note on or note off command
Serial.write(kpd.key[i].kchar);//send pitch data
Serial.write(100);//send velocity data
break;
case RELEASED:
msg = 128;
Serial.write(128);//send note on or note off command
Serial.write(kpd.key[i].kchar);//send pitch data
Serial.write(100);//send velocity data
}
Serial.print("Key ");
Serial.print(kpd.key[i].kchar);
}
}
}
} // End loop
//send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
Serial.write(command);//send note on or note off command
Serial.write(MIDInote);//send pitch data
Serial.write(MIDIvelocity);//send velocity data
}
I’ve tried changing the array const to a int providing no result even though this should not be the issue.
New contributor
ShadowyCollectibles is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.