#include #include #include #include #include #include #define SCREEN_ADDRESS 0x3C #define PPQN 24 //24 is good for triplets. each pulse is 1/512th note #define PULSE_LENGTH 5 #define INPUT_CONNECTED_PIN 12 #define INPUT_PIN 2 #define ENC_BTN_PIN 17 #define ENC_D1_PIN 4 #define ENC_D2_PIN 3 #define START_STOP_BTN_PIN 14 int outsPins[6] = {5, 6, 7, 8, 9, 10}; int outsValues[6] = {1, 2, 4, -4, 0, 0}; //positive - divide, negative - multiply, 0 - off /2 /3 /4 /5 /6 /7 /8 /16 /32 /random x2 x3 x4 x6 x8 x12 x16 x24 int outsPeriods[6]; int outsClocksCounts[6]; bool clockMode; bool clockModeOld; int inputMode; //clock, reset, start/stop int clockCount = 0; int bpm = 127; int pulseClockCount = 0; int pulseCount = 0; int pulsePeriod; bool isPlaying = 0; int needToResetChannel; bool pulseCounted = false; int displayTab = 0; int displayTabOld; bool needToChangeTab = 0; bool buttonPushed = false; bool changesSaved; int encPositionOld = 0; Adafruit_SSD1306 display(128, 64, &Wire, -1); RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03); void setup() { Serial.begin(9600); pinMode(INPUT_CONNECTED_PIN, INPUT_PULLUP); pinMode(ENC_BTN_PIN, INPUT_PULLUP); pinMode(START_STOP_BTN_PIN, INPUT_PULLUP); pinMode(INPUT_PIN, INPUT_PULLUP); //probably will need interrupt for (int i=0; i<6; i++) { pinMode(outsPins[i], OUTPUT); } display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); updateScreen(); updatePeriods(); FlexiTimer2::set(1, 1.0/1000, internalClock); // 1.0/1000 = 1ms period FlexiTimer2::start(); } void loop() { checkInputs(); } void internalClock() { if (isPlaying) { // pulse rules for dividers (multipliers should be in separate for loop, as this one occurs only on pulse 0 (bpm)) if (pulseCount == 0 && !pulseCounted) { for (int i = 0; i<6; i++) { if (outsValues[i] > 0) { //Dividers (>0) if (outsClocksCounts[i] == 0) { //Pulse on 0 digitalWrite(outsPins[i], HIGH); } if (outsClocksCounts[i] < (outsValues[i]-1)) { outsClocksCounts[i]++; if (i == 0) {Serial.println(outsClocksCounts[i]);} } else { outsClocksCounts[i] = 0; } } } pulseCounted = true; } //lets get internal pulse going if (pulseClockCount == 0) { pulseCount++; pulseCounted = false; } if (pulseClockCount < pulsePeriod) { pulseClockCount++; } else { pulseClockCount = 0; } if (pulseCount >= PPQN) { pulseCount = 0; } // pull low all outputs after set pulse length if (pulseClockCount >= PULSE_LENGTH) { for (int i = 0; i<6; i++) { digitalWrite(outsPins[i], LOW); } } } } void updatePeriods() { pulsePeriod = 60000 / (bpm * PPQN); } void resetClocks() { for (int i = 0; i<6; i++) { outsClocksCounts[i] = 0; } } void checkInputs() { //input jack switcch clockMode = digitalRead(INPUT_CONNECTED_PIN); if (clockMode != clockModeOld) { updateScreen(); clockModeOld = clockMode; } //encoder button if (needToChangeTab == 0 && digitalRead(ENC_BTN_PIN) == 0) { needToChangeTab = 1; } else if (needToChangeTab == 1 && digitalRead(ENC_BTN_PIN) == 1) { displayTabOld = displayTab; displayTab++; if (displayTab>6) { displayTab = 0; } needToChangeTab = 0; updateScreen(); } //encoder encoder.tick(); int encPosition = encoder.getPosition(); if (encPositionOld != encPosition) { int change = encPositionOld - encPosition; if (displayTab == 0) { bpm = bpm + change; } else { outsValues[displayTab-1] = outsValues[displayTab-1] + change; needToResetChannel = displayTab-1; } updateScreen(); encPositionOld = encPosition; } //play button if (!digitalRead(START_STOP_BTN_PIN) && !buttonPushed) { isPlaying = !isPlaying; resetClocks(); buttonPushed = true; } else if (digitalRead(START_STOP_BTN_PIN) && buttonPushed) { buttonPushed = false; } } void updateScreen() { display.clearDisplay(); //Tabs display.setCursor(0,0); display.setTextSize(1); if (displayTab == 0) { display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); display.print(F(" ")); display.setTextColor(SSD1306_WHITE); display.print(F(" bpm ")); } else { display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); display.print(F(" bpm")); } for (int i = 1; i <= 6; i++) { if (displayTab == i) { display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); display.print(" "); display.setTextColor(SSD1306_WHITE); display.print(" "); display.print(i); display.print(" "); } else { display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); display.print(" "); display.print(i); } } display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); display.println(F(" ")); display.println(); //Content display.setTextSize(3); display.setTextColor(SSD1306_WHITE); if (displayTab == 0) { display.print(bpm); display.println(F("bpm")); } else { if (outsValues[displayTab-1]==0) { display.print(F("OFF")); } else if (outsValues[displayTab-1]>0) { display.print(F("/")); display.print(abs(outsValues[displayTab-1])); } else { display.print(F("x")); display.print(abs(outsValues[displayTab-1])); } } display.display(); }