Event Based Midi Read

Hi Guys,

So I stripped out all the serial commands and set pin 6 to respond to the clock signals with a high pulse that lasts 1ms. Midi clock signals do not have an on and off component like midi note signals so I had to add the 1ms delay to allow my oscilloscope trigger to pick up the signal and let me do timing measurements.

Unfortunately it seems that the time variation is still there, though now it is smaller (the clock pulses vary from 20-27ms apart, it used to be 14-25ms). Here's a video where every time I hit the measure button the scope takes a time measure between the rising edges, which is printed at the bottom in yellow.

https://www.youtube.com/edit?o=U&video_id=JkzSw-U4EdU

I guess the next step is to measure the clock signal from some other music software like Ableton and see if the variation disappears. If so then Traktor is to blame instead of the Arduino.

Here is my code if you are interested.

#include "MIDIUSB.h"

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(6, OUTPUT);
}

int t_old = 0;

void loop() {
  midiEventPacket_t rx;

  do {
    rx = MidiUSB.read();

    // read the velocity value
    int Velocity = rx.byte3;
    
    if (rx.header != 0) {

      if (Velocity == 127){
        digitalWrite(LED_BUILTIN,HIGH);
       
        digitalWrite(6,HIGH); // triggers on clock signals
        delay(1);
       
        } else {
        digitalWrite(LED_BUILTIN,LOW);
        

        }

      }
  } while (rx.header != 0);

   digitalWrite(6,LOW);

}

Thanks for the Serial tips Mike. The character count was always the same so I guess the buffer must be the cause.

Ad