Sync external device's clock from arduino wia midi

i am sending clock msg to my synth to inc or dec tempo of synth while playing sound.
below code increasing decreasing tempo very well. but ,before getting changes in tempo, the tempo return from low bpm .and hold key sound is disturbed.
i means, if tempo is still 120. i push button to inc. tempo will be 47 or 48.then it will go to 121. my hold key sound's tempo will be affected badly before going to 121.
does here idea to up/down clock smoothly or update it at specify bpm without disturbing hold key sound or running beat tempo.?????

``

#include <Wire.h>
#include <Adafruit_SSD1306.h> // testing....
#include <Adafruit_GFX.h>

#define OLED_ADDR 0x3C // SDA = A4 , SCL =A5

const int midiClock = 0xf8; // midic clock pulse byte
const int startByte = 0xfa; // midi clock start byte
const int stopByte = 0xFC; // midi clock stop byte
const uint8_t NOTE_OFF = 0x80;
const uint8_t NOTE_ON = 0x90;
const uint8_t KEY_PRESSURE = 0xA0;
const uint8_t CC = 0xB0;
const uint8_t PROGRAM_CHANGE = 0xC0;
const uint8_t CHANNEL_PRESSURE = 0xD0;
const uint8_t PITCH_BEND = 0xE0;

Adafruit_SSD1306 display(-1);
const int plus = A1;
const int reset = A2;
const int save = A3;

int P1 = 0;
int Last_P1=0;
int P2=0;
int Last_P2=0;
int P3=0;
int Last_P3=0;

unsigned long old_millis = 0;
int tempo = 145;

void setup() {

pinMode(plus, INPUT_PULLUP);
pinMode(reset, INPUT_PULLUP);
pinMode(save, INPUT_PULLUP);
Serial.begin(31250);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
}
void loop () {

unsigned long new_millis = micros();
unsigned long interval = (60000000/(tempo*24));

 P1 = digitalRead(plus);
 P2 = digitalRead(reset);
 P3 = digitalRead(save);

   if (P1 != Last_P1) {
if (P1 == LOW) 
{    tempo++;       screen  (); } 
else
{     }
Last_P1 = P1;

}

// --------------------------------------------------------------------------
if (P2 != Last_P2) {
if (P2 == LOW) { tempo--; screen (); }
else { }
Last_P2 = P2;
}
//---------------------------------------------------------------------------
if (P3 != Last_P3) {
if (P3 == LOW) { tempo = 100; screen (); }
else { }
Last_P3 = P3;
}

if (new_millis - old_millis >= interval) {  old_millis = new_millis;    Serial.write(midiClock);  }     //     send midi clock every interval

}

   void screen  ()   {    

display.clearDisplay();
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(1, 1);
display.print(tempo);
display.display();
}

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