i think you are about use of millis(); function. but i have tried it in this code
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
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;
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();
}
as in above code , when Midi clock is increased or decreased, the Running Rhythm beat or hold key sound's tempo is cracked badly with ultra low tempo.
Now am going to follow this example using two timers timer1 & Timer2.
Arduini Due timer
but Timer Due library for only Arduino Due board
And i am searching for two timer Library For arduino nano board
please suggust me or give hint to make (arduino nano midi clock sync device) . if possible ??????