Can't understand why I getting correct sync BPM with a "magic" value

I'm trying to sync a MIDI external clock to a sequencer I've made with a display and a raspberryPi Pico.

I got everything working but there is something I don't understand.

The code:

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>

using namespace midi;

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI_DIN)
void setup(){
     MIDI_DIN.begin(0);
}

void loop(){
   if (MIDI_DIN.read())
    {
        // switch (mode ? MIDI_USB.getType() : MIDI_DIN.getType())
        switch (MIDI_DIN.getType())
        {
        case midi::Clock:
        {
            static int ticks = 0;
            if (ticks++ >= clockDiv)
            {
                static int sequencerIndex = 0;
                Serial.println(sequencerIndex++);
                if(sequencerIndex > 15){
                     sequencerIndex = 0;
                }
                ticks = 0;
            }

            static unsigned long previousClock = 0;
            unsigned long currentClock = micros();
            static int bpmTicks = 0;
            if (bpmTicks++ >= 24)
            {
                // Calcul du delta entre les pulses MIDI Clock
                unsigned long delta = currentClock - previousClock;

                bpm = (bpm + (63000000 / delta)) / 2; // 1us in sc => 1000000, delta in us, by minute (1000000/delta) * 60

                previousClock = currentClock;
                bpmTicks = 0;
            }

            break;
        }
    }
}

This code is working when syncing with an external machine (Digitakt).

The problem is, the sequencerIndex is perfectly synced with the external machine step ONLY if the clockDiv is equal to 5, and I can't understand why this number.

When I try to double or divide this clockDiv by a 2 factor, the sequencer is not speeded or slowed by 2. The spped doesn't seem to be synced anymore to the external gear.

that is odd since your ticks counts up to clockDiv before incrementing sequencerIndex so doubling clockDiv should half how fast sequencerIndex is incrementing.

But, since you didn't show your entire sketch, who knows?

One thing to consider is what is the baud rate of Serial? You have your Serial.println() in there associated with incrementing sequencerIndex and if you have it set to something slow, like 9600, there is a good chance that the Serial output buffer is filling up faster than it can be emptied which causes your .println() to block until there is space in the buffer available.

Calling begin() from the MIDI library automatically set the baudrate to 31250 I think

serialMIDI.h


struct DefaultSerialSettings
{
    /*! Override the default MIDI baudrate to transmit over USB serial, to
    a decoding program such as Hairless MIDI (set baudrate to 115200)\n
    http://projectgus.github.io/hairless-midiserial/
    */
    static const long BaudRate = 31250;
};

void begin()
	{
        // Initialise the Serial port
            mSerial.begin(Settings::BaudRate);
	}