In my last post I described how a Bluetooth connection between Arduino and a Windows 8.1 device can be established.
The next step for me was to connect the Arduino to my electronic drum kit which has both, a MIDI-IN and a MIDI-OUT jack, but any other electronical instrument will do as well.
The wiring diagram for an Arduino Uno R3 with MIDI-IN/OUT and the JY-MCU Bluetooth module is shown in Fig.1. NOTE: Occasionally there are MIDI shields available for Arduino, so you might not have to build it on your own.
The Arduino code to relay MIDI>Bluetooth and Bluetooth>MIDI is actually quite simple.
//======================================================authorship //by Michael Osthege (2013) //======================================================includes #include "SoftwareSerial.h" //======================================================constants const int TX_BT = 10; const int RX_BT = 11; const int MIDI_TX = 1; const int MIDI_RX = 0; //======================================================bluetooth setup SoftwareSerial btSerial(TX_BT, RX_BT); //======================================================initialization void setup() { Serial.begin(31250); btSerial.begin(9600); Serial.println("Bluetooth initialized"); } //======================================================do work void loop() { ReadMIDI();//listen on the MIDI-IN ReadBluetooth();//listen for Bluetooth } void ReadMIDI() { if (Serial.available() > 0)//there's something in the buffer { char buffer[3]; Serial.readBytes(buffer, 3);//receive it btSerial.write(buffer[0]);//relay it btSerial.write(buffer[1]); btSerial.write(buffer[2]); } } void ReadBluetooth() { if (btSerial.available() > 0)//there's something in the buffer { char buffer[3]; btSerial.readBytes(buffer, 3);//receive it Serial.write(buffer[0]);//relay it Serial.write(buffer[1]); Serial.write(buffer[2]); } }
As I understand it, the MIDI protocol communicates with commands of three bytes. Therefore I decided to relay all incoming serial messages in chunks of three bytes.
To test the relay, I modified the BluetoothConnectionManager of my previous example to send/receive chunks of 3 bytes as MIDI commands too. Incoming commands are printed into lines of text and simple sounds can be sent as well.
You can try it out yourself in this sample application: http://code.msdn.microsoft.com/Arduino-as-a-MIDIBluetooth-1c38384a
Have a nice weekend =)