A simple midi sync to analog clock converter

I am slowly building analog synth modules for myself, and one of the most basic stuff one soon needs is a way to sync a computer to analog sequencers and so on. After looking around google, I couldn't find anything I liked enough, so I wrote a few lines of code to do it.
Midi uses a "clock" byte (0xf8) to sync various devices-for every quarter note, 24 "clocks" are sent serially. In order to sync an analog sequencer to that, one has to extract the clock bytes, then output a trigger signal (ie, the "analog clock") for every 24 bytes received. That division number (24 in our case) can be changed to reflect various rythm values (1/4, 1/8 and so on), either with a use of a potentiometer (adding the respective code yourself) or with an external frequency divider IC.

This particular example is tailored to control a simple CD4017 based sequencer, which can be found all over the internet. CD4017 expects a clock signal on one pin, and advances to the next step when a rising edge is received. In order to extend the musical usefulness of it, reset function is implemented as well. When you start the midi arrangement, arduino will output a reset signal (going to the reset pin of the 4017), so that the sequencer step is always synced to the song.
It utilises the midi library and the midi input is wired using an optocoupler, as demonstrated in the midi protocol specifications.
Connections needed are:
Optocoupler out--->Rx
CD4017 Clock pin ---> A0
CD4017 reset pin ---> (A1 OR Qx), where Qx is the corresponding step of the CD4017. For a 4step sequencer, that's Q4 on the datasheet.

INH corresponds to "clock enable" pin on the 4017, and for now is not used. i think it's not needed at all, for most basic sequencing functions.

In order for syncing to be as sharp as possible, I tried to keep all delays to a minimum. trT controls the duration of the clock trigger signal. I found that 1 microsecond is enough to keep it working, but you may need to experiment, depending on the board and external IC used. If however one needs to make the pulse substantially longer, I suggest using an external monoflop IC (like the HC123) to adjust the length as needed and still not bloat the system with unneeded delay.

#include <MIDI.h>
/*
This program listens for MIDI clock bytes on the Rx pin and translates them to analog clock format with a note value division of your choice, while controling a basic CD4017 step sequencer.
*/

#define CLK A0   		// Clock 
#define RST A1                  // Synced Reset 
#define INH A2                  // Inhibit
#define LED 13                  // On Board LED
#define trT 1                   // Trigger pulse duration
#define nDiv 24                 // Rythm Value of the Clock. 24=1/4, 12=1/8, 6=1/16, 3=1/32                 


void trCLK(){
   digitalWrite(CLK,HIGH);
   delayMicroseconds(trT);
   digitalWrite(CLK,LOW);
}

void trRST() { 	
    digitalWrite(RST,HIGH);
    delayMicroseconds(trT);
    digitalWrite(RST,LOW);
}


void setup() {
   pinMode(LED, OUTPUT);
   pinMode(CLK, OUTPUT);
   pinMode(RST, OUTPUT);
   MIDI.begin();
   digitalWrite(RST,LOW);
}

int i=0; //clock counter

void loop() {
  if (MIDI.read()) {                    // Is there a MIDI message incoming ?
    switch(MIDI.getType()) {		// Get the type of the message we caught
      case Clock:               // If it is a Clock byte
        i++;                     
        if (i==nDiv){              //Note Value Division
          trCLK();          
          i=0;
          break;
        }
        break;
      case Start:
        trRST();
        i=1; 
      break;
    }
  }

}

I think this is it, pretty much. What else can be done to make sure it will work as tightly as possible? How can I theoretically calculate the time needed from the generation of the clock bit on the master clock, until the the output pulse? (I could just use an oscilloscope, I know. don't have any atm).

I say try it and see!

I'm interested to know how you get on with this project. I'm an Arduino noob myself, and am just starting on a similar project, albeit one with a few more bells and whistles.

a|x