Skip to content

Instantly share code, notes, and snippets.

@billautomata
Created May 23, 2019 15:47
Show Gist options
  • Save billautomata/96550178c55a3818fe31b3ca0500d4dd to your computer and use it in GitHub Desktop.
Save billautomata/96550178c55a3818fe31b3ca0500d4dd to your computer and use it in GitHub Desktop.
midi clock and 16th notes test program
unsigned long BPM = 120;
unsigned long PPQ = 24;
unsigned long PPQ_T;
unsigned long SECONDS_IN_MINUTE = 60;
unsigned long MICROSECONDS_PER_PPQ = (SECONDS_IN_MINUTE * 1000000) / (BPM * PPQ);
unsigned long _micros;
unsigned long MICROSECONDS_PER_STEP = (SECONDS_IN_MINUTE * 1000000) / (BPM * 4);
unsigned long STEP_T;
bool isNoteOn;
void setup() {
Serial.begin(56000);
PPQ_T = micros();
STEP_T = micros();
_micros = micros();
isNoteOn = false;
}
void loop() {
_micros = micros();
if(_micros - PPQ_T >= MICROSECONDS_PER_PPQ) {
PPQ_T = _micros;
Serial.write((int)248);
}
if(_micros - STEP_T >= MICROSECONDS_PER_STEP) {
STEP_T = _micros;
if(isNoteOn == true) {
Serial.write((uint8_t)128); // note OFF
Serial.write((uint8_t)60); // note value
Serial.write((uint8_t)64); // note velocity
isNoteOn = false;
}
Serial.write((uint8_t)144); // note ON
Serial.write((uint8_t)60); // note value
Serial.write((uint8_t)64); // note velocity
isNoteOn = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment