93 lines
2.5 KiB
C++
93 lines
2.5 KiB
C++
//needs debouncing and investigation of pin 0
|
|
|
|
#include <MIDIUSB.h>
|
|
|
|
static const int matrixInPins[] = {1, 0, 2, 3, 4, 5, 6, 7}; //x
|
|
static const int matrixOutPins[] = {8, 9, 10, 16, 14, 15, 18}; //y
|
|
int channel = 1;
|
|
int midiNotes[7][8] = {
|
|
// 1 0 2 3 4 5 6 7
|
|
{ 0, 0, 76, 69, 80, 70, 83, 45}, //8
|
|
{ 0, 0, 77, 0, 82, 0, 73, 0}, //9
|
|
{ 0, 0, 0, 49, 0, 0, 0, 0}, //10
|
|
{ 0, 0, 51, 0, 46, 0, 48, 47}, //16
|
|
{ 0, 0, 0, 0, 0, 0, 0, 52}, //14
|
|
{24, 22, 79, 0, 81, 0, 75, 50}, //15
|
|
{25, 23, 78, 71, 0, 72, 74, 84} //18
|
|
};
|
|
int noteToSend = 0;
|
|
int noteSent = 0;
|
|
int noteToStop = 0;
|
|
int noteStopped = 0;
|
|
int scanIn;
|
|
int scanOut;
|
|
|
|
void setup() {
|
|
Serial1.end();
|
|
for (int i = 0; i < sizeof(matrixInPins); ++i)
|
|
pinMode(matrixInPins[i], INPUT_PULLUP);
|
|
for (int i = 0; i < sizeof(matrixOutPins); ++i) {
|
|
pinMode(matrixOutPins[i], OUTPUT);
|
|
digitalWrite(matrixOutPins[i], HIGH);
|
|
}
|
|
}
|
|
|
|
void scan() {
|
|
for (scanOut = 0; scanOut < 7; ++scanOut) {
|
|
digitalWrite(matrixOutPins[scanOut], LOW);
|
|
for (scanIn = 0; scanIn < 8; ++scanIn) {
|
|
if (digitalRead(matrixInPins[scanIn]) == LOW) {
|
|
//Serial.print(matrixInPins[scanIn]);
|
|
//Serial.print(":");
|
|
//Serial.println(matrixOutPins[scanOut]);
|
|
noteToSend = midiNotes[scanOut][scanIn];
|
|
//Serial.println(noteToSend);
|
|
sendNote();
|
|
}
|
|
if (digitalRead(matrixInPins[scanIn]) == HIGH && midiNotes[scanOut][scanIn] == noteSent) {
|
|
noteToStop = noteSent;
|
|
stopNote();
|
|
}
|
|
}
|
|
digitalWrite(matrixOutPins[scanOut], HIGH);
|
|
}
|
|
}
|
|
void sendNote() {
|
|
if (noteToSend != noteSent) {
|
|
noteOn(channel, noteToSend, 64);
|
|
MidiUSB.flush();
|
|
noteSent = noteToSend;
|
|
}
|
|
}
|
|
void stopNote() {
|
|
if (noteToStop != noteStopped) {
|
|
noteOff(channel, noteToStop, 64);
|
|
//allNotesOff(channel, noteToStop, 64);
|
|
//controlChange(123, 0, channel);
|
|
MidiUSB.flush();
|
|
noteStopped = noteToStop;
|
|
noteToStop = 0;
|
|
noteToSend = 0;
|
|
noteSent = 0;
|
|
}
|
|
}
|
|
|
|
void noteOn(byte channel, byte pitch, byte velocity) {
|
|
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
|
|
MidiUSB.sendMIDI(noteOn);
|
|
}
|
|
|
|
void noteOff(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);
|
|
}
|
|
|
|
void loop() {
|
|
scan();
|
|
}
|