Hello! Best regards from Chile, hope you can help me to understand this...
i will try to think and write in english, please excuse me.
Well i'm trying to develope a MIDI tap tempo device. The main goal is to detect the time between button presses and from that estimate the frequency that MIDI message 0xF8 should be sent.
I'm using interrupts and TIMER1. I understand that when I increase the prescaler value, the 'resolutión' decrease... So i've read on the forum that it's a good advice to use de lower prescaler value that is possible. So i'm using 8 as prescaler. If i use the 1024 prescaler... The same compare match register value works for several BPM values.
i'm using this formula to calculate the value of the compare match register with which interrupts are triggered.

So, 120BPM value equals to a 2Hz frequency, then the compare match register value is 41666,625. When i use this value it works pretty well! The BPM values goes from 119.5 to 120.56.
But when I try it with a high frequency, like 290 BPM (that is 4.8333Hz or actually other frequencies that are float), the compare match register value calculated is "17242,52". But then, the BPM loose accuracy. The BPM values goes from 288.5 to 291.5.
This happens because the 24 MIDI messages are not send on a consistent frequency. The intervals are not being respected as in this an example. When the interval isn't respected I understand that the BPM value increases or decreases.

I know that the variation is small, could be negligent either but i would like to know first, why this happens, and then...is there a way to fine tune this value... I´m using arduino Uno/Nano. Should i go for a more powerfull board like the STM32 with a faster core clock?
Later i would like to make a device that stores BPM values and when user press a button, the device send the MIDI clock to a slave device...But the idea is that the slave device detects "290" no 289 or 291. Something like this:
By the way... Is there a chance that this issue is due to the fact that i'm using a software to create a virtual midi port and 'hearing' the serial wreited midi message by another sotware (metronome)?
I dont have a physic device to try this ...
Here is my code:
boolean toggle1 = 0;
void setup() {
Serial.begin(96000);
//set pins as outputs
cli();//stop interrupts
//set timer1 interrupt at 1Hz
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 17242; //Using the formula to acchieve 290BPM
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
//Stop Timer
TCCR1B &= 0B11111000;
//Start Timer
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
}//end setup
ISR(TIMER1_COMPA_vect) { //timer1 interrupt 1Hz toggles pin 13 (LED)
//generates pulse wave of frequency 1Hz/2 = 0.5kHz (takes two cycles for full wave- toggle high then toggle low)
Serial.write(0xF8);
}
void loop() {
//do other things here
}
Thanks,thanks so much! I really want to understand and improve this!

