MIDI Library Clocking

I have been digging through all kinds of forms and so forth for the past 2 weeks or so in my quest to build a MIDI Step Sequencer. Just for fun.

I want it to take in MIDI Clock to sync to my host DAW and then put out note values based on buttons that trigger an interrupt.

I have read the Deitel book on C++ and coded in KSP (the language used for Native Instruments Kontakt) but I consider this my first “real” go at coding. Using handles and Callbacks is new to me.

This code almost works. On my first attempt I went with the standard reading serial port approach. But my issue is that the first beat of this code doesn’t line up with the first beat of my host.

I have played back at very slow tempos (like 10bpm) to figure out exactly when the note triggers and have found that no amount of delays or clock offsets seem to work. If I use a count off then I can adjust but I want to start up right in time. I also understand that the midi start command is supposed to be what supplies this sync. It runs in sync just fine, but it doesn’t respond fast enough.

Code: SEE ATTACHMENT (it pushed me over the character limit of the post)

So I tried a bunch of stuff trying to locate what it is that causes the first tic to be delayed. I determined to switch methods to using the MIDI Lib.

BUT, when I plug in the arduino and open my DAW (FL in my case but I have others that produce similar results) its fine, UNTIL I hit play, then it freezes up and produces all kinds of random output. So I have something fundamentally wrong with my code here. I think I don’t fully understand the clockIn command of the midi lib, so I went digging through the wiki http://arduinomidilib.fortyseveneffects.com/a00043.html#ga80da13308ec9c54c522cb1f3b78d8332

This is also the first time I have used “methods”.

I must have a misunderstanding somewhere.

This is the code: SEE ATTACHMENTS (it pushed me over the character limit of the post)

On this code I tried commenting out various parts to locate the problem and have been struggling to identify it.

If anyone could tell me what why it produces such strange output I would greatly appreciate it! I would like to use the second code just because it’s methodology seems more clear to me as a general structure.

Also if anyone knows why I miss the first clock tic on the arduino in the first code example and has an idea to fix it that would be greatly appreciated. I have tried just about anything I could think of, offsets, latency compensation, using various note trigger values, and also just coding in “Make a note on the first tic” which also was delayed. (I know its delayed because I put in a not in the DAW to check if they are line up). I further tried to eliminate any problems by changing the PPQ of the DAW to 24 (which shouldn’t matter but I wanted to eliminate any possible difference).

So, I am stumped and humbly ask for help! Thank-you for any wisdom you have.

Midi_In_Sequencer_HardCode_VS5.ino (11.6 KB)

Midi_In_Sequencer_MIDI_LIB_VS7.ino (11.7 KB)