Counting MIDI tick- Tempo LED flickers out of sync

Hi, Im haveing trouble counting midi clock ticks. Im am trying to make a LED flash in sync to the beat, like every 8th note. I can get the light to flash but it is more of a random flicker and definitely not in step. Also the LED will not start flashing until the seguencer (Masrter -sending the midi ) has been started,stopped and restarted like 3-6 times. (This happens after every restart and I have pretty much ruled out a continuity problem) Im using this to recognize the midi clock ticks:

byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;

void setup() {
Serial.begin(31250);
}

void loop() {
if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
}
else if((data == midi_clock) && (play_flag == 1)) {
Sync();
}
}
}

void Sync() {
// do something for every MIDI Clock pulse when the sequencer is running
}

(the original sketch can be found at http://little-scale.blogspot.com/2008/05/how-to-deal-with-midi-clock-signals-in.html )

And I have this as void Sync(); to count the ticks and do something every set number of ticks:

void Sync() {
if(counter ==2 ) {
counter = 0;
digitalWrite(13, HIGH);
}

else if(counter == 1) {
counter = counter + 1;
digitalWrite(13, LOW);
}

else {
counter = counter + 1;
}
}

(this was found on the same page further down in the comments)

Ive tried a bunch a time divisions and different ways to count the ticks ( like a "for" statement) but it always flickers and usually a start up problem. Short delays seem to help just a little. This seems like it should be pretty basic, is there something Im missing? BTW- i am adding this to an existing midi to cv sketch. can be found here: http://registeringdomainnamesismorefunthandoingrealwork.com/blogs/?p=51 The original functions continue to work fine but could it possibly interfere with the clock count?

Sorry if this was covered before. Thanks in advance

Also the LED will not start flashing until the seguencer (Masrter -sending the midi ) has been started,stopped and restarted like 3-6 times.

I think this is because of the input routine you have is getting the midi message out of sync.

What you need to do is to start over gathering bytes if any byte is not what you expect it to be. You can see an example of what I mean if you look at the code for this project:- http://www.thebox.myzen.co.uk/Hardware/Glockenspiel.html

So just to clarify....the counting/flash routine is probably working, but the routine that recognizes the clock ticks is throwing things out of sync?

If thats so... I should change the input routine to run somethingmore like this, right?: ( blink without delay)

const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Update— Mike you are right! The clock input was interfering with the original MIDI2CV sketch, in which it was inserted. I ran this solo and it seems to work as expected:

int counter = 0;
int output_pin = 13; // set output pin
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;
int play_flag = 0;
byte data;

void setup() {
Serial.begin(31250);
pinMode(output_pin, OUTPUT);
}

void loop() {
if(Serial.available() > 0) {
data = Serial.read();
if(data == midi_start) {
play_flag = 1;
}
else if(data == midi_continue) {
play_flag = 1;
}
else if(data == midi_stop) {
play_flag = 0;
}
else if((data == midi_clock) && (play_flag == 1)) {
Sync();
}
}
}

void Sync() {
if(counter == 23) {
counter = 0;
digitalWrite(output_pin, HIGH);
}

else if(counter < 23) {
counter = counter + 1;
digitalWrite(output_pin, LOW);
}

else {
counter = counter + 1;
}
}

I had the change the count to 23, every 24 was causing the flash to be slightly out of sync. There are 24clock ticks in a quarter note…is this caues I reset to counter to 0 and not 1?

So i just need to figure out how to add the clok tick routine without messing up the timing. easier said than done. I have to admit that I am guilty of adhering to the “cut and paste” school of sketch development… sometimes I get in over my head- midi stuff especially.