Skip to content

Instantly share code, notes, and snippets.

@equaliser
Last active July 3, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equaliser/92142feeeef9b9247fcd046171a99ac9 to your computer and use it in GitHub Desktop.
Save equaliser/92142feeeef9b9247fcd046171a99ac9 to your computer and use it in GitHub Desktop.
#include <digitalWriteFast.h>
// din sync to midi
// pins
#define DINSYNC_STARTSTOP 3
#define DINSYNC_CLOCK 2
// midi pin 5 > 220ohm resistor > D1
// midi pin 2 > ground
// midi pin 4 > 220ohm resistor > +5V
// leds
#define LED_CLOCK 7
#define DINSYNC_STARTSTOP_OFF 5
#define DINSYNC_CLOCK_OFF 8
// midi commands
#define MIDI_BYTE_CLOCK 248
#define MIDI_BYTE_START 250
#define MIDI_BYTE_STOP 252
#define LED_FLASH_TIME 10 // in milliseconds, hopefully
boolean clockJustHappened = false;
volatile unsigned long clockCount = 0;
boolean startStopJustHappened = false;
boolean startStop = false;
boolean isClockLEDOn = false;
unsigned long clockLEDFlashTimer = 0;
boolean isMidiClockOn = true;
unsigned long midiBeatCount = 0;
void setup()
{
pinMode(LED_CLOCK, OUTPUT);
pinMode(DINSYNC_STARTSTOP, INPUT);
pinMode(DINSYNC_CLOCK, INPUT);
Serial.begin(31250);
attachInterrupt(digitalPinToInterrupt(DINSYNC_STARTSTOP), dinsyncStartStopChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(DINSYNC_CLOCK), dinsyncClockChange, CHANGE);
}
void dinsyncStartStopChange() {
if (startStop == false) {
if (digitalReadFast(DINSYNC_STARTSTOP) == HIGH) {
startStop = true;
startStopJustHappened = true;
}
} else {
if (digitalReadFast(DINSYNC_STARTSTOP) == LOW) {
startStop = false;
startStopJustHappened = true;
}
}
}
void dinsyncClockChange() {
if (digitalReadFast(DINSYNC_CLOCK) == HIGH) {
clockJustHappened = true;
clockCount = clockCount + 1;
} else {
clockJustHappened = false;
}
}
void loop()
{
if (startStopJustHappened && startStop) {
// send midi clock out
Serial.write(MIDI_BYTE_START);
startStopJustHappened = false;
clockCount = 0;
checkClockLEDOn();
} else if (clockJustHappened && startStop) {
// } else if (clockJustHappened && startStop && clockCount % 2 == 0) { // possible divide by 2 for 48ppm Korg DIN sync
Serial.write(MIDI_BYTE_CLOCK);
clockJustHappened = false;
// flash beat light
checkClockLEDOn();
} else if (startStopJustHappened && !startStop) {
Serial.write(MIDI_BYTE_STOP);
startStopJustHappened = false;
clockJustHappened = false;
turnClockLEDOff();
clockCount = 0;
// turn off running light
}
checkClockLEDOff();
}
// **************************************************
// switching LED output
void checkClockLEDOn() {
if (((clockCount - 1) % 12) == 0){
turnClockLEDOn();
}
}
void turnClockLEDOn() {
digitalWriteFast(LED_CLOCK, HIGH);
clockLEDFlashTimer = millis();
isClockLEDOn = true;
}
void checkClockLEDOff() {
if (isClockLEDOn && (millis() - clockLEDFlashTimer) >= LED_FLASH_TIME) {
turnClockLEDOff();
}
}
void turnClockLEDOff() {
digitalWriteFast(LED_CLOCK, LOW);
isClockLEDOn = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment