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 ??????
sorry,,,
i can not understand large coding of timer (TimerInterrupt:) for nano
please told about a such timer library inwhich can be written very simple in as in below example
You can not have serial printing from within a Timer Interrupt isr. Have the isr set a flag and print/clear the flag in loop().
Rather than trying to use the more complicated generic library, I would use the TimerOne and the msTimer2 libraries. Both libraries have examples for periodic interrupts.
#include "TimerOne.h"
#include <MsTimer2.h>
void setup()
{
Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
MsTimer2::set(500, flash); // 500ms period
MsTimer2::start();
}
void loop()
{
// your program here...
if (??? ==????) { Timer1.start(); }
if (??? ==????) { MsTimer2.start(); }
if (??? ==????) { Timer1.stop(); }
}
void callback () { something1 about Serial.write }
void flash () { something2 about read button state }
Looks about right (except for the Serial.write() in the ISR) but what happens when you try it?
The Serial.write() command does not execute from within the ISR and will happen after the code exits from the ISR. It's not clear to me that the situation you saw with the millis() timer will not happen as well when using the timer interrupt ISR.
Therefore, I have doubts that the timer interrupt version of code will work better than the millis() timer version in Post #3.
The linked Due code did not have the oled display, and I think it is the blocking aspects of the display which disrupt the midi performance.
Instead of understanding timer interrupt complicated coding, can I use millis() to make a pocket midi clock sync device from Nano board+rotary encoder or buttons
If yes , why it not working in post#3.
How will it work?? What part code