Hi folks,
I tinkered a while with my Arduino based arpeggiator now and continuously fail to make it MIDI sync capable. My attempt is based on simple counting between two timestamps of the clock signal. I experience two issues
a) the beat calculated has a huge jitter (e.g. about 10% in the below pasted example1 of the debug output for an input of 120)
b) the beat calculated does not increase beyond 186bpm if sending higher clock rates as in example2 (here: 360bpm input)
Extract of the debug output for example1:
tickTime: 19320
tickBeat: 129
tickTime: 23156
tickBeat: 107
tickTime: 18872
tickBeat: 132
tickTime: 22740
tickBeat: 109
Extract of the debug output for example2:
tickTime: 13440
tickBeat: 186
tickTime: 13440
tickBeat: 186
tickTime: 13440
tickBeat: 186
tickTime: 13440
tickBeat: 186
Do you have a clue about the issue? Thanks in advance for having a look into the code added below.
#include <Arduino.h>
#define DEBUG 1
// Variables for midi clock
unsigned long tickBeat;
unsigned long tickTime;
unsigned long currentTick;
unsigned long previousTick;
byte static midi_start = 0xfa;
byte static midi_stop = 0xfc;
byte static midi_clock = 0xf8;
byte static midi_continue = 0xfb;
byte data;
void setup() {
tickBeat = 0;
tickTime = 0;
currentTick = 0;
previousTick = 0;
Serial.begin(31250);
}
void loop() {
if(Serial.available()>0) {
data = Serial.read();
if((data==midi_clock)) {
currentTick = micros();
tickTime = (currentTick - previousTick);
tickBeat = 60000000 / (tickTime * 24);
previousTick = currentTick;
#ifdef DEBUG
Serial.print("-----------\n");
Serial.print("tickTime: ");
Serial.print(tickTime );
Serial.print("\n");
Serial.print("tickBeat: ");
Serial.print(tickBeat );
Serial.print("\n");
#endif
}
}
}