2010-11-07

2) Sending MIDI Notes

To send MIDI commands we need a 5 pin DIN jack, like for the MIDI-in, but without a galvanic decoupling. The jack has to be connected to +5V, the serial output (TX) and to ground (via a 220 Ohm resitor).

Parts list
-2 x Resistor 220 Ohm
-1 x Resistor 100 kOhm
-1 x Diode
-1 x opto coupler IC GNY17-2
-2 x 5-pole DIN female connector (180°)
-1 x MIDI cable
-1 x MIDI sync master (here: Roland TR-505)
-1 x MIDI sync slave (here: DIY synthesizer with MIDI-input)
-1 x Arduino (here: Arduino Mega. For this demo also Arduinos with less input and output channels will work)

Schematics













Software

The following code can reveive MIDI clock and send synchronous MIDI notes.


// Declaration of Varialbes
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;
int clock_step;
int note;

// Initialization
void setup() {
Serial.begin(31250);
clock_step=0;
note = 0x3F;
}

// Main Programm
void loop() {
if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
clock_step=0;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
clock_step=0;

sendMidiNote (0x80, note, 0x7F); //last note off
}
else if((data == midi_clock) && (play_flag == 1)) {
Sync();
}
}
}


// Functions

void Sync() { // play 8 fixed 16th notes, repeat after the cycle is finshed
clock_step = clock_step+1;
if (clock_step==1){ //1st step
sendMidiNote (0x80, note, 0x7F); //last note off
note = 0x5A;
sendMidiNote (0x90, note, 0x7F); //note of this step on
}
if (clock_step==7){ //2nd step
sendMidiNote (0x80, note, 0x7F); //last note off
note = 0x4A;
sendMidiNote (0x90, note, 0x7F);
}
if (clock_step==13){ //3nd step
sendMidiNote (0x80, note, 0x7F); //last note off
note = 0x4B;
sendMidiNote (0x90, note, 0x7F);
}
if (clock_step==19){ //4nd step
sendMidiNote (0x80, note, 0x7F); //last note off
note = 0x3F;
sendMidiNote (0x90, note, 0x7F);
}
else if (clock_step==24){
clock_step=0;
}
}

void sendMidiNote (byte midiCommand, byte noteValue, byte velocityValue){
Serial.print(midiCommand, BYTE);
Serial.print(noteValue, BYTE);
Serial.print(velocityValue, BYTE);
}




No comments:

Post a Comment