IWEUA.COM
Generating MIDI with
Arduino
A friend explained that on a particular TV show he worked the Talent would hit a large button to open the show.
The button was not actually connected to anything, a runner watching the talent and giving a verbal cue over
talkback, except sometimes the cue was missed.

Could I wire the switch to an Arduino to send a MIDI command to the lighting desk to initiate the lighting sequence
and fire an audio track from the Audio desk ?

Probably I said.... I have worked with MIDI and DMX over the years but never studied the protocol until now.

There are lots of resources online
, and MIDI is a complex protocol, so this is strictly a first introduction.
Some code :

#define BitWidth                26
#define StartStopBitPeriod 24
#define MidiOutPin1           8
#define NoteOnChannel     B10010011           // 0x90 channel 1 decimal ( 1 binary starting at Zero )
#define NoteToPlay           B00111100           // 60 Decimal 0x3C
#define NoteOnVelocity     B01010101           // 0x64
#define NoteoffVelocity      B01111111           // 0x40
#define NoteOffChannel     B10000011           // 0x80 channel 1

void setup()
{
 Serial.begin(9600);
 pinMode    ( MidiOutPin1, OUTPUT);
 digitalWrite ( MidiOutPin1, HIGH);
}

void loop()
{
       if ( Something )       SendMidiMessage(NoteOnChannel,NoteToPlay,NoteOnVelocity);

       if ( SomethingElse ) SendMidiMessage(NoteOffChannel ,NoteToPlay,NoteoffVelocity);
}

//************************************************************
boolean SendMidiMessage(byte ByteOne, byte ByteTwo, byte ByteThree)
{
  WriteDataBitsOut(ByteOne);
  WriteDataBitsOut(ByteTwo);
  WriteDataBitsOut(ByteThree);
}

//************************************************************
void WriteDataBitsOut(byte ByteToWrite)

{
 WriteStartBit();
 for ( int BitCount = 0; BitCount < 8; BitCount++)              // LSB First
{
  digitalWrite( MidiOutPin1, (bitRead ( ByteToWrite , BitCount )) );
  delayMicroseconds(BitWidth);
}
 WriteStopBit();
}

//************************************************************
void WriteStartBit()
{
  digitalWrite(MidiOutPin1,LOW);
  delayMicroseconds(StartStopBitPeriod);
}
//************************************************************

void WriteStopBit()
{
  digitalWrite(MidiOutPin1,HIGH);
  delayMicroseconds(StartStopBitPeriod);
}
I make no excuses, the code above is fairly rubbish. This is strictly a 'get you started' example.

The code above worked, though one would never normally use delays to define Bit lengths.

In real time code ( like the code above ), one tries to avoid delays entirely.

This application was super simple, read a switch press, send a Midi message, make sure the switch is
de-bounced and make sure a hysteresis window is coded to ensure that you don't send multiple 'NOTE ON'
messages with a noisy switch.

Ensuring the Arduino electrical interfacing to the MIDI device is correct is very important :

https://www.midi.org/specifications/item/midi-din-electrical-specification

A comprehensive guide can be founds here :

http://midi.teragonaudio.com/tech/midispec.htm  

MIDI-OX is a very useful tool in combination with a PC's MIDI I/O device for MIDI I/O
Be very careful when interfacing to your MIDI device. The 220R resistor pulling pin 4 to the MIDI Device's
Opto LED high is vital.

Failure to limit the current through the MIDI device's opto isolator could easily cause this device to fail,
bricking your MIDI instrument's input stage. Please be careful.

A MIDI message timing example : Bit number and Bit timing transition in mS : Click for bigger picture