Last active
March 6, 2018 19:45
-
-
Save borogove/40059894d1b35b7c6e1ce8f94edd579e to your computer and use it in GitHub Desktop.
Arduino MIDI in handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
byte currentData[3]; | |
byte currentCommand = 0; | |
byte commandChannel = 0; | |
byte currentNote = 255; | |
short length = 0; | |
bool midiFlicker = false; | |
void updateMidi() | |
{ | |
digitalWrite(PIN_LED_RED, midiFlicker ? LOW : HIGH ); | |
while (Serial.available() > 0) | |
{ | |
midiFlicker = !midiFlicker; | |
byte midiByte = Serial.read(); | |
// Command byte? | |
if (midiByte & 0x80) | |
{ | |
currentCommand = midiByte & 0xF0; | |
commandChannel = midiByte & 0x0F; | |
length = 0; | |
} | |
else | |
{ | |
// Data byte | |
if (length < 3) | |
{ | |
currentData[length++] = midiByte; | |
} | |
switch (currentCommand) | |
{ | |
case 0x80: // note off | |
if (length == 2) | |
{ | |
noteOff(currentData[0],currentData[1]); | |
length = 0; | |
} | |
break; | |
case 0x90: | |
if (length == 2) | |
{ | |
noteOn(currentData[0],currentData[1]); | |
currentNote = currentData[0]; | |
length = 0; | |
} | |
break; | |
case 0xC0: | |
if (length == 1) | |
{ | |
programChange(currentData[0]); | |
length =0; | |
} | |
break; | |
} | |
} | |
} | |
} | |
void noteOn( byte note, byte velocity ) | |
{ | |
// ... do something with the note on | |
} | |
void noteOff( byte note, byte velocity ) | |
{ | |
// ... do something with the note off | |
} | |
void programChange( byte prog ) | |
{ | |
// ... do something with the program change | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment