Go Down

### Topic: A simple 'Send MIDI clock' example (Read 9468 times)previous topic - next topic

#### deseipel

##### Nov 23, 2013, 06:21 am
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?

#### capicoso

#1
##### Nov 25, 2013, 04:50 am

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.

#### deseipel

#2
##### Nov 25, 2013, 08:25 amLast Edit: Nov 25, 2013, 09:53 am by deseipel Reason: 1
I'm using the MIDI library, but it would seem as simple as this, right?

Code: [Select]
` 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.`

Code: [Select]
`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);}}`

#### deseipel

#3
##### Nov 25, 2013, 09:54 am
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.

#4
##### Jan 03, 2014, 06:56 pm
Finally I found someone who did already what I need :-)
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

#### capicoso

#5
##### Jan 04, 2014, 07:08 pm

Finally I found someone who did already what I need :-)
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);

#### ErikOostveen

#6
##### Feb 20, 2017, 10:03 pm
Hi - have a look here for an Arduino midi clock with analog outputs:

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

#### wagman45

#7
##### May 23, 2020, 09:40 pm
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)

#### MarkT

#8
##### May 26, 2020, 01:34 pm
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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up