(HELP!) Problem with MIDI clock

Hi, I'm trying to make a simple midi clock so I can synchronize one of my guitar pedal to it.

The program is not complete yet, I'm just testing to see if it works first. This code works ok at 120 bpm. The LED on my pedal is synchronized with the LED on my breadboard, but when I try a lower bpm (60), my pedal can't synchronize at all...

I know that midiclock is 24 messages per beat. When I put 60 bpm, I can see with MIDI-OX that it takes 1 sec to have 24 messages.

The pedal works really well when I send the MIDI clock from my computer at 60 bpm. If I take the midi output from my computer and put it in the input, MIDI-OX reads this:

If I put the output of the Arduino MIDI clock into my computer, it reads this:

the same thing...

Do any of you know what could cause my pedal to not sync at a lower BPM with the arduino? It works really well with my computer.

Also, I am a beginner so any tips to improve my code is appreciated! :slight_smile:

Here is the code:

//Metronome with MIDI clock

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
using namespace midi;

void setup()
{  
  pinMode(8, OUTPUT); //LED
  pinMode(6, INPUT); //On-Off button
  MIDI.begin(31250);
}

int LED = 8;
int BOUTON = 6;
int NbPulse;
bool OnceRun;
bool OnceStop;

void ClockStart()
{
  MIDI.sendRealTime(0xFA);
} 

void ClockPulse()
{
  MIDI.sendRealTime(0xF8);
} 

void ClockStop()
{
  MIDI.sendRealTime(0xFC);
} 

void LedPulse ()
{ 
  digitalWrite (LED, HIGH);
  delay(5);
  digitalWrite (LED, LOW);
}

void loop() 
{
  if (digitalRead (BOUTON))
  {
    if (OnceRun == 0)
    {
    ClockStart();
    }   
    if (NbPulse == 0)
    {
    LedPulse();
    }
    ClockPulse();  
    delay(17); //This is where the value for the bpm will be (17ms = 150 bpm)
    if (NbPulse < 23)
    {
    NbPulse = (NbPulse + 1);
    }
    else
    {
    NbPulse = 0; 
    }
    OnceRun = 1;
    OnceStop = 0;
  }
  else
  {
    if (OnceStop == 0)
   {
    ClockStop();
   }  
  NbPulse = 0;
  OnceRun = 0;
  OnceStop = 1;
  } 
}

I also tried this very simple code at 60 bpm. It works better, but it still can't properly synchronize.


//Metronome with MIDI clock

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
using namespace midi;

void setup()
{  
  MIDI.begin(31250);
}

void loop() 
{        
  MIDI.sendRealTime(0xF8); 
  delay(42); //This is where the value for the bpm will be (42ms = 60 bpm) 
}

How is your Arduino connected to your MIDI stack ?

It is connected with a standard MIDI DIN cable and I used this wiring:

I used 2 X 220 ohm resistors

The MIDI library is wrongly started, the correct is

MIDI.begin(inChannel);  // default inChannel = 1

The correct connections are


Source: MIDI.org

Are the 5 & 4 pin inverted? A lot of wiring diagrams have the 5V wire on the 5th pin. Also, some wiring diagram have an inverter like this one:
midi-schem

Which one is correct?

Where, in MIDI-in?
No, only the R changes pin, but it doesn't matter.

This schematic solved my problem :)))))))