A simple 'Send MIDI clock' example

is sending midi clock a simple matter of:

defining tempo (could hard coded, set by a midi file that's played, or set by a dial or button, etc)

Using the tempo to know how many times, per quarter note, to send midi clock (decimal 248, hex 0xF8)

Using an actual timer (millis()) to keep track of when a quarter note has passed (based on tempo).

For instance, if the tempo was 60 bpm. That would mean that for every second that's passed, which is also every quarter note, I'd have to send a midi clock message?

deseipel:
For instance, if the tempo was 60 bpm. That would mean that for every second that's passed, which is also every quarter note, I'd have to send a midi clock message?

No. You have to send 24 ticks every quarter note

You can do it with millis() yes.

I'm using the MIDI library, but it would seem as simple as this, right?

 long bpm = 120;
 long tempo = 1000/(bpm/60);
 
 long prevmillis = 0;
 long interval = tempo/24;    //interval is the number of milliseconds defined by tempo formula.
void loop () {

  MIDI.read(); //is there incoming MIDI?
unsigned long currentMillis = millis();
if(currentMillis - prevMillis > interval) {
    // save the last time.
    prevMillis = currentMillis;
MIDI.SendRealtime(clock);
}
}

wow, this appears to work! I initially forgot to divide my interval by 24 though. I tested this on a couple of midi devices and MIDI ox and it works. I'll need some further testing though.

Finally I found someone who did already what I need :slight_smile:
I have programmed a metronome and want to give it a MIDI clock function as well to give the tempo to other instruments like the microKorg which has a MIDI in. I'm still trying to understand how the MIDI Clock from the Arduino midi library works. Do you think you can explain your code a little bit more? Did you connect it like in this sketch? http://arduino.cc/en/uploads/Tutorial/MIDI_bb.png

ladio:
Finally I found someone who did already what I need :slight_smile:
I have programmed a metronome and want to give it a MIDI clock function as well to give the tempo to other instruments like the microKorg which has a MIDI in. I'm still trying to understand how the MIDI Clock from the Arduino midi library works. Do you think you can explain your code a little bit more? Did you connect it like in this sketch? http://arduino.cc/en/uploads/Tutorial/MIDI_bb.png

You just need to understand how midi clock works. You don't need the library. Midi clock sends a specific byte 24 times per quarter note. That byte is decimal 248. Everytime you want to send a midi tick, this does it:
Serial.write(248);

Hi - have a look here for an Arduino midi clock with analog outputs:

http://www.erikoostveen.co.uk/#anchor2

First, thank you so much for creating this post! It was super helpful for a project I'm working on. One thing I noticed is that for odd-numbered tempos, (I was using 75BPM) it was helpful to add a ".0" to all of the numbers, and to change prevmillis and interval to floats.

long bpm = 75.0;
long tempo = 1000.0/(bpm/60.0);

float prevmillis = 0;
float interval = tempo/24.0; //interval is the number of milliseconds defined by tempo formula.

if(playing==1){
unsigned long currentMillis = millis();
if(currentMillis - prevmillis >= interval) {
//save the last time.
prevmillis = currentMillis;
MIDI.sendClock();
// Serial.print(interval);
// Serial.print('\n');
}
}

(my code starts or stops the clock off and on with a button press, hence the if statement)

capicoso:
No. You have to send 24 ticks every quarter note

You can do it with millis() yes.

But using micros() is going to be noticably more accurate.