I've created an electronic instrument that uses a MPR121 to read fingerholes. A timer scans for input 256 times a second. This would be fingerholes in manual mode or MIDI.read if in MIDI player mode. Normal operation would be battery powered without a USB connection, however, if there is a USB connection the device can be placed into a MIDI playing mode where the finger holes are disabled for the duration. I see the MIDI.begin but no MIDI.end. What happens if the USB cable is disconnected given I've defined and started a MIDI channel? Would it be enough to prevent the MIDI.read from being executed if I detect that the USB has been disconnected and the instrument is back to playing via battery. If it works properly, the teensy will not lose power so I need to make sure the remnants of the MIDI won't hang the teensy given there is no USB.
Code:
MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI);
in setup ()
MIDI.begin(MIDI_CHANNEL_OMNI);
usbMIDI.setHandleNoteOff(MIDINoteOff);
usbMIDI.setHandleNoteOn(MIDINoteOn);
in a timer routine that scans for inputs 256 times a second
if (MIDIState) <-true if the instrument allows MIDI input, right now this doesn't mean the USB is up, just that the player has put the instrument in MIDI player mode
{
usbMIDI.read();
}
/**************************************************************************************************************
Turn midi On
***************************************************************************************************************/
void MIDINoteOn(byte channel, byte note, byte velocity)
{
if (MIDIPlaying == false) <- this section of code unmutes the instrument if we are actively playing
{
MIDIPlaying = true;
instrumentUnmuteAll();
}
if (MIDIState == true) {
newNotePlaying = MIDItoNote(note); <- this converts the midi note number to a note that can be played through the audio timer code
MIDIInactivityCounter = 0; //reset the counter so we don't cut off sounds
}
} //end MIDINoteOn
/**************************************************************************************************************
Turn midi Off
***************************************************************************************************************/
void MIDINoteOff(byte channel, byte note, byte velocity)
{
if (MIDIPlaying == false)
{
MIDIPlaying = true;
instrumentUnmuteAll();
}
MIDIInactivityCounter = 0; //reset the counter so we don't cut off sounds
} //end MIDINoteOff[
/CODE]