The Teensy can send MIDI data over USB. This include sending MIDI note on and note off events. Built-in functions make sending MIDI data simple. A summary of Teensy MIDI message types that can be sent over USB can be found here.
In the first example, the hardware used is a single push button connected to a digital pin.
The Bounce2 library is used to read the state changes of the button. usbMIDI.sendNoteOn() and usbMIDI.sendNoteOff() are used to send the MIDI data over USB.
A MIDI variant of the USB type sub menu under the Tools menu must be selected before uploading the sketch to the Teensy.
The digital pin is setup to be an input pin with a pullup resistor to avoid erroneous digital reads.
Example 1 - Sending a MIDI Note from One Push Button
In this example, a single push button is used to send a MIDI note. When the button is pressed, a note on event is sent via USB. When the button is depressed, a note off event is sent via USB. Note the format of the usbMIDI.sendNoteOn() function in terms of arguments is note, velocity, channel. These arguments can be substituted with static values, variables.
Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Sending_MIDI_Notes_Example_1/
Example 2 - Sending MIDI Notes from Two Push Buttons
In this example, two push buttons are used to send two MIDI notes. When a button is pressed, a note on event is sent via USB. When a button is depressed, a note off event is sent via USB. The setup can hold more than one note at once.
Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Sending_MIDI_Notes_Example_2/
The Teensy can easily send note on and note offs using the usbMIDI.sendNoteOn() and usbMIDI.sendNoteOff functions.