Im hoping I could get some help figuring out how to properly setup this matrix to scan a piano keyboard for MIDI notes. Im using a Teensy 2.0++ a 74LS42 to set the 8 column outputs low one at a time and a 4069 to invert the signal coming from the rows. This code has a bug were if I press more than one button on a column the other columns will register button presses are the shared rows. I cant seem to isolated the problem but any guidance to where Ive made a mistake would be greatly appreciated!
//MIDI//////////////////////////////////////////////////////////////////////////////////////////////////////////
const uint8_t keys = 64;
uint8_t keyToMidiMap[keys];
bool noteOnState[keys];
const int noteVelocity = 127;
void noteOn(int cmd, int pitch, int velocity);
//SETUP///////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
for (byte i = 0; i < keys; i++) { // Map scan matrix buttons/keys to actual Midi note number. Lowest num 41 corresponds to F MIDI note.
keyToMidiMap[i] = i + 36;
noteOnState[i] = false;
}
DDRC |= B11111111;//SCAN MATRIX OUTPUT PINS
DDRF = 0; //SCAN MATRIX INPUT PINS
Serial.begin(31250);
delay(1000);
}
//MAIN/////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
//COLUMN CONTROL/////////////////////////////////////////////////////////////////////////////////////////////////////////
static byte col = 0;// The 74LS42 (IC58) shift up to next column
col++;
if (col > 8) col = 0;
//Serial.println (col);
PORTC &= B10000111;
PORTC |= (col << 3);
//delay(1000);
//KEYBOARD NOTES ON/OFF/////////////////////////////////////////////////////////////////////////////////////////////////////////
if (col <= 7) {
for (byte b = 0; b < 8; b++) {// process if any combination of keys pressed
if (PINF & (1 << (b))){
if (! noteOnState[col * 8 + b]){
noteOn(0x91, keyToMidiMap[col * 8 + b], noteVelocity);
noteOnState[col * 8 + b] = true;
}
}
else {
if ( noteOnState[col * 8 + b]){// process if any combination of keys released
noteOn(0x91, keyToMidiMap[col * 8 + b], 0);
noteOnState[col * 8 + b] = false;
}
}
}
}
//PANEL SWITCHES/////////////////////////////////////////////////////////////////////////////////////////////////////////
if (col == 8) {//Scan for Transpose/ARP and key assign/ ARP range/ Arp on/ Keytrans
}
}
//MIDI NOTE SEND/////////////////////////////////////////////////////////////////////////////////////////////////////////
void noteOn(int cmd, int pitch, int velocity) {
//Serial.print("MIDI SEND: ");
Serial.print(" CMD:");
Serial.print(cmd);
Serial.print(" PITCH:");
Serial.print(pitch);
Serial.print(" VELO:");
Serial.println(velocity);
}