Im turning my old casio ctk 230 49 keys keyboard into a midi one using Arduino pro micro,
im using a multiplexer 16 pins.
i have scanned the matrix of the keyboard using multimeter and have figured this out
it has 15 pins, matrix is 7*8 (7 rows and 8 columns)
| Row\Col | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|
| 1 | C2 | C#2 | D2 | D#2 | E2 | F2 | F#2 | G2 |
| 2 | G#2 | A2 | A#2 | B2 | C3 | C#3 | D3 | D#3 |
| 3 | E3 | F3 | F#3 | G3 | G#3 | A3 | A#3 | B3 |
| 4 | C4 | C#4 | D4 | D#4 | E4 | F4 | F#4 | G4 |
| 5 | G#4 | A4 | A#4 | B4 | C5 | C#5 | D5 | D#5 |
| 6 | E5 | F5 | F#5 | G5 | G#5 | A5 | A#5 | B5 |
| 7 | C6 | — | — | — | — | — | — | — |
now i have connected the rows to the Arduino board, where 1-7 pins are connected to the Arduino 2-9 pins.
the columns from 8-15 are connected to the mux from c15-18(pin 8 connecting on c15 and pin 15 on c8)
initially it was working but some keys weren't working and the were playing off keys i think it has something to do with the code.
please help with the code.
#include midi.h
// Pin Definitions
// Rows are connected to Arduino pins 2–8
const int row1 = 2;
const int row2 = 3;
const int row3 = 4;
const int row4 = 5;
const int row5 = 6;
const int row6 = 7;
const int row7 = 8;
// MUX control pins
const int SIG = A1;
const int S0 = 15;
const int S1 = 14;
const int S2 = 16;
const int S3 = 10;
// mux channel order C15 → C8
int muxChannels[8] = {15,14,13,12,11,10,9,8};
uint8_t keyToMidiMap[56];
boolean keyPressed[56];
int noteVelocity = 127;
// select mux channel (REPLACES shift register scanColumn)
void scanColumn(int columnIndex)
{
int ch = muxChannels[columnIndex];
digitalWrite(S0, ch & 1);
digitalWrite(S1, (ch >> 1) & 1);
digitalWrite(S2, (ch >> 2) & 1);
digitalWrite(S3, (ch >> 3) & 1);
pinMode(SIG, OUTPUT);
digitalWrite(SIG, LOW);
delayMicroseconds(30);
}
void setup()
{
// MIDI mapping from your chart EXACTLY
// Row 1
keyToMidiMap[0] = 36;
keyToMidiMap[1] = 37;
keyToMidiMap[2] = 38;
keyToMidiMap[3] = 39;
keyToMidiMap[4] = 40;
keyToMidiMap[5] = 41;
keyToMidiMap[6] = 42;
keyToMidiMap[7] = 43;
// Row 2
keyToMidiMap[8] = 44;
keyToMidiMap[9] = 45;
keyToMidiMap[10] = 46;
keyToMidiMap[11] = 47;
keyToMidiMap[12] = 48;
keyToMidiMap[13] = 49;
keyToMidiMap[14] = 50;
keyToMidiMap[15] = 51;
// Row 3
keyToMidiMap[16] = 52;
keyToMidiMap[17] = 53;
keyToMidiMap[18] = 54;
keyToMidiMap[19] = 55;
keyToMidiMap[20] = 56;
keyToMidiMap[21] = 57;
keyToMidiMap[22] = 58;
keyToMidiMap[23] = 59;
// Row 4
keyToMidiMap[24] = 60;
keyToMidiMap[25] = 61;
keyToMidiMap[26] = 62;
keyToMidiMap[27] = 63;
keyToMidiMap[28] = 64;
keyToMidiMap[29] = 65;
keyToMidiMap[30] = 66;
keyToMidiMap[31] = 67;
// Row 5
keyToMidiMap[32] = 68;
keyToMidiMap[33] = 69;
keyToMidiMap[34] = 70;
keyToMidiMap[35] = 71;
keyToMidiMap[36] = 72;
keyToMidiMap[37] = 73;
keyToMidiMap[38] = 74;
keyToMidiMap[39] = 75;
// Row 6
keyToMidiMap[40] = 76;
keyToMidiMap[41] = 77;
keyToMidiMap[42] = 78;
keyToMidiMap[43] = 79;
keyToMidiMap[44] = 80;
keyToMidiMap[45] = 81;
keyToMidiMap[46] = 82;
keyToMidiMap[47] = 83;
// Row 7
keyToMidiMap[48] = 84;
pinMode(SIG, INPUT_PULLUP);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(row1, INPUT_PULLUP);
pinMode(row2, INPUT_PULLUP);
pinMode(row3, INPUT_PULLUP);
pinMode(row4, INPUT_PULLUP);
pinMode(row5, INPUT_PULLUP);
pinMode(row6, INPUT_PULLUP);
pinMode(row7, INPUT_PULLUP);
Serial.begin(31250);
delay(1000);
}
void loop()
{
for (int col = 0; col < 8; col++)
{
scanColumn(col);
int groupValue1 = !digitalRead(row1);
int groupValue2 = !digitalRead(row2);
int groupValue3 = !digitalRead(row3);
int groupValue4 = !digitalRead(row4);
int groupValue5 = !digitalRead(row5);
int groupValue6 = !digitalRead(row6);
int groupValue7 = !digitalRead(row7);
if (groupValue1 && !keyPressed[col]) {
keyPressed[col] = true;
noteOn(0x90, keyToMidiMap[col], noteVelocity);
}
if (groupValue2 && !keyPressed[col + 8]) {
keyPressed[col + 8] = true;
noteOn(0x90, keyToMidiMap[col + 8], noteVelocity);
}
if (groupValue3 && !keyPressed[col + 16]) {
keyPressed[col + 16] = true;
noteOn(0x90, keyToMidiMap[col + 16], noteVelocity);
}
if (groupValue4 && !keyPressed[col + 24]) {
keyPressed[col + 24] = true;
noteOn(0x90, keyToMidiMap[col + 24], noteVelocity);
}
if (groupValue5 && !keyPressed[col + 32]) {
keyPressed[col + 32] = true;
noteOn(0x90, keyToMidiMap[col + 32], noteVelocity);
}
if (groupValue6 && !keyPressed[col + 40]) {
keyPressed[col + 40] = true;
noteOn(0x90, keyToMidiMap[col + 40], noteVelocity);
}
if (groupValue7 && !keyPressed[col + 48]) {
keyPressed[col + 48] = true;
noteOn(0x90, keyToMidiMap[col + 48], noteVelocity);
}
// release
for (int r = 0; r < 56; r++)
{
if (!groupValue1 && keyPressed[col]) {
keyPressed[col] = false;
noteOn(0x90, keyToMidiMap[col], 0);
}
}
pinMode(SIG, INPUT_PULLUP);
}
}
void noteOn(int cmd, int pitch, int velocity)
{
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}