Guys, trying to figure out why my midi clock is running a bit slower. Can someone check if my math is correct? :-[
int bpm = 140;
#define PPQ 96
unsigned long ppqtime = 60000000 / (bpm*PPQ);
In C++ I would actually do like this:
float ppqtime = 60000000.0f / (float(bpm)*float(PPQ));
So I'm not sure if my Arduino code is correct or not...
Thanks, Wk
What are the results expected and what did you get?
Arduino compiler does floats (32 bits) so why
60000000 => 60000000UL
make it explicit an unsigned long to start with.
Nermind, my math for counting the PPQ and Beats was a bit wrong.
Here's the correct code for a very simple MIDI Clock with variable BPM and PPQ.
// Wusik.com (c) WilliamK 2010
// Serial 115200 //
boolean Started = false;
boolean badClock = false;
unsigned long startTime;
unsigned long currentTime;
unsigned long processedtime;
float BPM = 95.0f;
float PPQ = 96.0f;
float ppqtime;
float currentmicroTime;
float ppqmicroTime;
int beats = 0;
int ppqcounter = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (!Started)
{
Started = true;
ppqtime = 60000000.0f / (BPM*PPQ); // (60 x 1,000,000) / (BPM*PPQ)
Serial.print("BPM: ");
Serial.println(BPM);
Serial.print("PPQ: ");
Serial.println(PPQ);
Serial.print("Micros PPQ: ");
Serial.println(ppqtime);
beats = 0;
ppqcounter = 0;
startTime = millis();
ppqmicroTime = (float)micros() + ppqtime;
}
currentmicroTime = (float)micros();
if (currentmicroTime >= ppqmicroTime)
{
currentTime = millis();
ppqmicroTime += ppqtime;
ppqcounter++;
if (ppqcounter == PPQ)
{
ppqcounter = 0;
beats++;
if (beats == BPM)
{
processedtime = currentTime - startTime;
Serial.println("All BPM Done!");
Serial.print("Processed Time MS: ");
Serial.println(processedtime);
BPM += 25;
Started = false;
}
}
}
}
Here's the output from the Serial, which is 99.9% correct in terms of timing. 
BPM: 95.00
PPQ: 96.00
Micros PPQ: 6578.95
All BPM Done!
Processed Time MS: 60002
BPM: 120.00
PPQ: 96.00
Micros PPQ: 5208.33
All BPM Done!
Processed Time MS: 59996
BPM: 145.00
PPQ: 96.00
Micros PPQ: 4310.34
All BPM Done!
Processed Time MS: 59938
Best Regards, WilliamK