Midi filtertering of stop start messages

You need to reset clk when you begin so the LED is always on the first 25% of the beat.

// MIDI clock as per Little Scale
byte midi_start = 0xfa;
byte midi_stop = 0xfc;
byte midi_clock = 0xf8;
byte midi_continue = 0xfb;

static uint8_t clk = 0;


// Variables
int tempoled = 13;
int play_flag = 0;
byte data;


// Setup
void setup() {
  Serial.begin(31250);
  pinMode(tempoled, OUTPUT);
}


// Program
void loop() {

  if (Serial.available() > 0) {
    data = Serial.read();

    if (data == midi_start) { // if midi in start
      Serial.write(midi_start); // send midi out start
      play_flag = 1;
      clk=0;
    }

    if (data == midi_continue) { // if midi in continue
      Serial.write(midi_continue); // send midi out continue
      play_flag = 1;
      clk=0;
    }

    if (data == midi_stop) { // if midi in stop
      Serial.write(midi_stop); // send midi out stop
      play_flag = 0;
    }
    
    if ((data == midi_clock) && (play_flag == 1)) {
      clk++;
      clk = clk % 24;                                 // 24 clock messages per beat
      if (clk / 6) digitalWrite(tempoled, LOW);  // this should light it up for 25% of the time
      else digitalWrite(tempoled, HIGH);
      Serial.write(midi_clock); // send midi clock to out... Provides tempo!
    }
  }
}