Hello world,
I'm new to arduino and i recently started this project where i took an old casio keyboard and made it a midi controller using arduino Leonardo. It uses an 8X8 matrix and its connected to a 74HC595 shift register, and the project seems to be working fine except for one thing, the key connected to the 2nd digital pin overlaps with the next note connected to that same pin.
I had the same issue with other keys, however i fixed it by assigning the right pin for each (i did'nt notice i assigned the same pin to some of them), but in this case i dont know what am I doing wrong.`
I can show you my schematics and the code im using.
Thanks in advance.
#define NUM_ROWS 8
#define NUM_COLS 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 row1Pin = 2; //2
const int row2Pin = 3; //3
const int row3Pin = 4; //4
const int row4Pin = 5; //5
const int row5Pin = 6; //6
const int row6Pin = 7; //7
const int row7Pin = 8; //8
const int row8Pin = 9; //9
// 74HC595 pins
const int dataPin = 11;
const int latchPin = 12;
const int clockPin = 13;
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 = 24;
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);
pinMode(row5Pin, INPUT);
pinMode(row6Pin, INPUT);
pinMode(row7Pin, INPUT);
pinMode(row8Pin, 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);
rowValue[4] = digitalRead(row5Pin);
rowValue[5] = digitalRead(row6Pin);
rowValue[6] = digitalRead(row7Pin);
rowValue[7] = digitalRead(row8Pin);
// 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 <= 8)
{
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);
noteOnMIDI(0, keyToMidiMap[row][col], NOTE_VELOCITY); // Nueva Linea Leonardo
MidiUSB.flush();// Nueva Linea Leonardo
}
void noteOff(int row, int col)
{
noteOffMIDI(0, keyToMidiMap[row][col], NOTE_VELOCITY); // Channel 0, middle C, normal velocity
MidiUSB.flush();
}
void noteOnMIDI(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOffMIDI(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
`