How usb midi clock byte send and recieve

i am trying to send/recieve clock msg to/from my pc software using usb midi libray .
i tested this code to send clock . here (start ,stop) is working .
i was thinking tempo will be changed to (100BPM) according to my below code but clock msg did not any response in step sequencer 's current tempo


int BPM = 100;
int  incoming_BPM = 0;
unsigned long del = ((1000000/(BPM/60))/4)*24;
void setup() {
   Serial.begin(115200);
}
void start() {
  midiEventPacket_t start = {0x0F, 0xFA, 0, 0};     
  MidiUSB.sendMIDI(start);
}

void stop() {
  midiEventPacket_t stop = {0x0F, 0xFC, 0, 0};     
  MidiUSB.sendMIDI(stop);
}
void clock() {
  midiEventPacket_t clock = {0x0F, 0xF8, 0, 0};     
  MidiUSB.sendMIDI(clock);
}
void loop() {
 if (B1_State != LastB1_State)    
 {
        if (B1_State == LOW)
        { 
         for (int t=0; t<24; t++)    {
            clock();  MidiUSB.flush();    
            delayMicroseconds(del);
                            } 
        }
}

what is missing in my code ??
how to send clock byte packet right way?? means manually inc/ dec in current playing Tempo.??
& how to Receive clock byte from my current tempo of step sequencer. and then how to store it in variable "incoming_BPM" ??????

I don't know what exactly is missing in you code, but i don't think this (way) of calculating the interval at which you should send it is correct.

((1000000/(BPM/60))/4)*24;

If you want to send it 24 times per 'beat' ( not 'bar')
Now to keep in mind that actually you want to know what you 'beat' length is, and send 24 'ticks during that time, and then send the 25th at exactly the right time.
That means you should do the calculation in float and that you should do the rounding at the very end.
But if we go for an approximation of a tick length.
at 120BPM that would be 500ms per beat
If we are going to do this in integer (or long)
so

1000000UL * 60 / BPM / 24

when doing multiplication and divisions in Integer, you should do the multiplication first.
Now that would result in 20833us but delayMicroseconds() is not reliable at that number

Probably you should test it first with

for (int t=0; t<24; t++)    {
            clock();  MidiUSB.flush();    
            delay(21);
                            } 

and see how that works at the receiving end.
As i said, it is important to send the 25th (or first of the next beat) as accurately as possible to keep you devices in sync. Also keep in mind, most Midi programs don't respond to midi sync unless they have received 'start' first.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.