hello Arduino community,
I'm new to Arduino and I try to turn my 1983 organ keyboard into a MIDI keyboard using Arduino UNO
I followed a tutorial and manage to make everything work, except that the keys I press do not match the right note and I don't know how to change it because I'm a total newbie to Arduino code. I need some help to fix it.
Here is my Arduino project :
the code I use, which gives wrong notes to keys
#define NUM_ROWS 4
#define NUM_COLS 13
#define NOTE_ON_CMD 0x90
#define NOTE_OFF_CMD 0x80
#define NOTE_VELOCITY 127
#define SERIAL_RATE 31250
// Row input pins
const int row1Pin = 2;
const int row2Pin = 3;
const int row3Pin = 4;
const int row4Pin = 5;
// 74HC595 pins
const int dataPin = 6;
const int latchPin = 7;
const int clockPin = 8;
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
};
// define first note of keyboard (int note)
void setup()
{
int note = 31;
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(row1Pin, INPUT);
pinMode(row2Pin, INPUT);
pinMode(row3Pin, INPUT);
pinMode(row4Pin, INPUT);
Serial.begin(SERIAL_RATE);
}
void loop()
{
for (int colCtr = 0; colCtr < NUM_COLS; ++colCtr)
{
//scan next column
scanColumn(colCtr);
//get row values at this column
int rowValue[NUM_ROWS];
rowValue[0] = digitalRead(row1Pin);
rowValue[1] = digitalRead(row2Pin);
rowValue[2] = digitalRead(row3Pin);
rowValue[3] = digitalRead(row4Pin);
// process keys pressed
for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
{
if (rowValue[rowCtr] != 0 && !keyPressed[rowCtr][colCtr])
{
keyPressed[rowCtr][colCtr] = true;
noteOn(rowCtr, colCtr);
}
}
// process keys released
for (int rowCtr = 0; rowCtr < NUM_ROWS; ++rowCtr)
{
if (rowValue[rowCtr] == 0 && keyPressed[rowCtr][colCtr])
{
keyPressed[rowCtr][colCtr] = false;
noteOff(rowCtr, colCtr);
}
}
}
}
void scanColumn(int colNum)
{
digitalWrite(latchPin, LOW);
if (0 <= colNum && colNum <= 7)
{
shiftOut(dataPin, clockPin, MSBFIRST, B00000000); //right sr
shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum]); //left sr
}
else
{
shiftOut(dataPin, clockPin, MSBFIRST, bits[colNum - 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);
}
I know I should change the matrix of "keytoMidiMap" and make a table with the corresponding MIDI number notes but I have zero knowledge in coding so I'm totally lost
your help will be much appreciated
thanks a lot
PS: it seems like a matrix like this correspond to my keyboard but how to I include it in the code?
int values[4][13] = {
{0, 0, 0, 0, 0, 0, 41, 42, 43, 44, 45, 46, 47},
{0, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59},
{0, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71},
{84, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83}
};
the 44 key goes from F0 to C4
here is the matrix of wires on the organ keyboard
the scheme I use to plug everything
PS: I also want to set up a sustain switch on pin9 and do not know how to code this.