Hi, I got an organ keyboard and wanted to get a midi out to connect it to the PC, which I got to work but with the press of a only one key at a time.
The problem comes when two keys or more of the same row are pressed, it gives a strange or badly sounding key, however there are no problems when doing so when several in the same column coincide.
I have searched the internet to see if there was any solution and I have not found anything, I do not know if it will be a problem of the code or the circuit, I may be missing some diode, the tests I have done have not gone well, I leave the code and the circuit, of which I can put more images, thanks.
// #define NUM_ROWS 6
// #define NUM_COLS 11
#define NUM_COLS 4
#define NUM_ROWS 8
#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127
//MIDI baud rate
#define SERIAL_RATE 31250
// Pin Definitions
// Row input pins
const int COL1Pin = 2;
const int COL2Pin = 3;
const int COL3Pin = 4;
const int COL4Pin = 5;
//const int row5Pin = 6;
//const int row6Pin = 7;
// 74HC595 pins
const int dataPin = 8;
const int latchPin = 9;
const int clockPin = 10;
boolean keyPressed[NUM_ROWS][NUM_COLS];
uint8_t keyToMidiMap[NUM_ROWS][NUM_COLS];
// bitmasks for scanning columns
int bits[] =
{
B00000001,
B00000010,
B00000100,
B00001000,
B00010000,
B00100000,
B01000000,
B10000000
};
void setup()
{
int note = 36;
for(int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
{
for(int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
{
keyPressed[rowCtr][colCtr] = false;
keyToMidiMap[rowCtr][colCtr] = note;
note++;
}
}
// setup pins output/input mode
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(COL1Pin, INPUT);
pinMode(COL2Pin, INPUT);
pinMode(COL3Pin, INPUT);
pinMode(COL4Pin, INPUT);
//pinMode(row5Pin, INPUT);
// pinMode(row6Pin, INPUT);
Serial.begin(SERIAL_RATE);
}
void loop()
{
for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
{
//scan next column
scanrow(rowCtr);
//get row values at this column
int colValue[NUM_COLS];
colValue[0] = digitalRead(COL1Pin);
colValue[1] = digitalRead(COL2Pin);
colValue[2] = digitalRead(COL3Pin);
colValue[3] = digitalRead(COL4Pin);
// rowValue[4] = digitalRead(row5Pin);
// rowValue[5] = digitalRead(row6Pin);
// process keys pressed
for(int colCtr=0; colCtr<NUM_COLS; ++colCtr)
{
if(colValue[colCtr] != 0 && !keyPressed[rowCtr][colCtr])
{
keyPressed[rowCtr][colCtr] = true;
noteOn(rowCtr,colCtr);
}
}
// process keys released
for(int colCtr=0; colCtr<NUM_COLS; ++colCtr)
{
if(colValue[colCtr] == 0 && keyPressed[rowCtr][colCtr])
{
keyPressed[rowCtr][colCtr] = false;
noteOff(rowCtr,colCtr);
}
}
}
}
void scanrow(int rowNum)
{
digitalWrite(latchPin, LOW);
if(0 <= rowNum && rowNum <= 7)
{
shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //right sr
shiftOut(dataPin, clockPin, MSBFIRST, bits[rowNum]); //left sr
}
else
{
shiftOut(dataPin, clockPin, MSBFIRST, bits[rowNum-8]); //right sr
shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //left sr
}
digitalWrite(latchPin, HIGH);
}
void noteOn(int row, int col)
{
Serial.write(NOTE_ON_CMD);
Serial.write(keyToMidiMap[row][col]);
Serial.write(NOTE_VELOCITY);
}
void noteOff(int row, int col)
{
Serial.write(NOTE_OFF_CMD);
Serial.write(keyToMidiMap[row][col]);
Serial.write(NOTE_VELOCITY);
}

