You probably will want to handle MIDI reception in an interrupt routine of your own. Likely you will also want to have a means to look up the time of reception of the Midi clock bytes ... this may need some tinkering.

Once you have two times from the difference of thosetimes you can calculate the needed delay for your sequencer.

To get a good reference clock you may need a timer interrupt, and the tinkering will be in what to do when you get a time overflow - or maybe you will find a trick to avoid overflows.

That should be about it, in principle._________________Jan
(yawning shifts perceived pitch, making things more interesting)

Ok, thanks. I'll try everything...
Searching i found this:
http://little-scale.blogspot.com.ar/2008/05/how-to-deal-with-midi-clock-signals-in.html
I thought i could do something like, put the note events inside void sync(). And if i want to send a note every 1/4, i would put a counter inside the sync() when the counter reachs 24(1/4 right?) it sends the note, if i want to send at 1/8 note, it'd count to 12 and then send it and reset to 0. Does this make sense? The idea would be that that 24, 12, 8 and etc, are dynamic, so if step one is at 1/4 duration, it counts to 24, then if the next one is at 1/8 duration it counts to 12...

Ok, thanks. I'll try everything...
Searching i found this:
http://little-scale.blogspot.com.ar/2008/05/how-to-deal-with-midi-clock-signals-in.html
I thought i could do something like, put the note events inside void sync(). And if i want to send a note every 1/4, i would put a counter inside the sync() when the counter reachs 24(1/4 right?) it sends the note, if i want to send at 1/8 note, it'd count to 12 and then send it and reset to 0. Does this make sense? The idea would be that that 24, 12, 8 and etc, are dynamic, so if step one is at 1/4 duration, it counts to 24, then if the next one is at 1/8 duration it counts to 12...

I haven't tried this, but couldn't you use midi library callbacks to update your sequencer clock based on the incoming midi clock data? So far as I can see, no need for you to write interrupt handlers.
Steve

I would have to read about callbacks... Will check it in the reference

Hey, i modified the sketch a lot, it had some redundant things and some other non-senses... Now it's much better, i just wanted to see if i could get the time accuracy better but nope. I'm using a timer on puredata via midi to compare the time, i don't know precise it's. But with a simple sketch that sends midi note, delay(250), and nothing else, it was the same... So i'm happy because i thought i was doing something very wrong.
The question, i know i won't be able to make a perfect clock, it's not my objetive, since i'll sync it to midi clock. But could a crystal oscillator make it better, than the ceramic resonator my board has?

Could it be that the variation in your counter is due to some variation in the serial buffer? I seem to remember reading something about that and also a way to see how much is in the buffer. I will see if I can find the reference.

I'll check those links.
But i think the variation is because the midi clock isn't 100% precise, luckily. I searched for a external clock for midi "atomic clock" 5000usd
Now i changed the led part and it was sending a note on on every 23 tick.

So i finally tested with my ears and it's all in sync, so the one to blame is the midi clock, but as long as they're all synced... The only weird thing is that the note on at speeds over 120bpm, sometimes it's a bit shorter so i have to put a bit of relase in my ADSR to hear it. In the oscilloscope when connected to the led i saw that too, sometimes the pulse is a bit shorter, this would be annoying. But in my actual sketch, the pulse will send a noteOff, so it won't bother, and the note on will last 24 ticks if its a 1/4, 12 if its 1/8, and so on.
I thought maybe that my optocoupler isn't fast enough or something? I'm using the whitey one 4n28. It was the first result that google returned when searching for midi in arduino. But when i was putting it all together i read that it was the worst option, and the 6n138 would be better...
About the interrupts, i'm not using anymore, i thought i really needed them but no... the thing was that i needed the note on to send just a pulse, and not constantly inside the loop(). But i added a counter that sums +1 when the note on is sent, and when the counter is = 1, the note off is sent. This way it's just a pulse. The code is a lot cleaner now, if you're interested i post it here

please do capicoso!
i am not very capable of understanding the arduino at this stage.
i'm always trying but so far not a lot of success

is it usb midi you are syncing as well?
i have a nano i want to use as slave & have had problems where to modify my code..
i have a 6n138 setup too as well as inbuilt usb and wonder if i can use a midi cable to slave & still output usb midi to PC??

please do capicoso!
i am not very capable of understanding the arduino at this stage.
i'm always trying but so far not a lot of success

is it usb midi you are syncing as well?
i have a nano i want to use as slave & have had problems where to modify my code..
i have a 6n138 setup too as well as inbuilt usb and wonder if i can use a midi cable to slave & still output usb midi to PC??

it doesn't do anything new like the midi clock or anything, it's the same code but optimized
I'm not using usb, just standar midi din and now adding midi in with 4n28 opto. Did you try the hiduino firmware?
Right now in the code midi in is not implemented
i'm experimenting it now. My latest code(not uploaded yet) has midi clock in, and the sequence syncs to the midi clock, also i added a trasposition pot for the sequence. The schematic will be available soon

Yes, i solved it. Thanks though
The code is similar... The counter goes +1 every tick, and a if statement for the note off and on events. Length potentiometers controls the variable at which number the counter resets to 0. So if step 1 is 12, and 2nd step is 24, it'd play a 1/8 and then a 1/4 note. Right now i'm not on my computer, i'll upload this part of the code. The rest is the same

at every clock tick, the function clock is called. Durnota is the note duration, defined by each of the potentiometers(this is in the main loop). They're fixed values like 11, 22, ...
When the counter starts at 0 it sends the noteon event, and depending on the durnota value it'll last longer or shorter. Then it resets to 0 and on to next step...

Quite simple but powerful if you don't need to read other MIDI message like noteOn or NoteOFF

It seems to me that if you stored the 1st byte and tested it for being a midi timing byte, you could pass that first byte to a second midi handler that processed two-byte and three-byte midi commands.
Steve

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum

Please support our site. If you click through and buy from our affiliate partners, we earn a small commission.