#include #include #include #include #include #include #define SCREEN_ADDRESS 0x3C #define PPQN 24 #define PULSE_LENGTH 5 #define MAXBPM 250 //250 at 24ppqn with 5ms pulse will be 50/50 square wave #define MINBPM 20 #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 #define ANALOGUE_INPUT_1_PIN A1 const int outsPins[6] = {5, 6, 7, 8, 9, 10}; const int outsModes[22] = {-24, -16, -12, -8, -6, -4, -3, -2, -1, 0, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 16, 32, 100}; //positive - divide, negative - multiply, 0 - off, 100 - random int outsModeIndex[6] = {10, 8, 12, 14, 9, 8}; //10 - /1, 9 - off int outsPeriods[6]; int outsClocksCounts[6]; int outsModesPlay[6]; //actual channel modes array updated from outsModeIndex each beat bool clockMode; bool clockModeOld; int inputMode; //clock, reset, start/stop int clockCount = 0; int bpm = 130; int pulseClockCount = 0; int pulseCount = 0; int pulsePeriod; bool isPlaying = 0; int needToResetChannel; bool beatCounted = false; bool pulseCounted = false; int displayTab = 0; int displayTabOld; bool needToChangeTab = 0; bool buttonPushed = false; int a1Input = 0; 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(START_STOP_BTN_PIN, ANALOGUE_INPUT_1_PIN); 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(); updatePeriod(); FlexiTimer2::set(1, 1.0/1000, internalClock); // 1.0/1000 = 1ms period FlexiTimer2::start(); } void loop() { checkInputs(); } void internalClock() { if (isPlaying) { // Action on each pulse if (pulseClockCount == 0 && !pulseCounted) { //divider if (pulseCount == 0 && !beatCounted) { for (int i = 0; i<6; i++) { outsModesPlay[i] = outsModes[outsModeIndex[i]]; //updated here to prevent sync problems for multipliers if (a1Input != 0 && i == 5) { //TESTING THE MODULATION ON 6th CHANNEL outsModesPlay[i] = outsModes[outsModeIndex[i] - a1Input]; } if (outsModesPlay[i] > 0 && outsModesPlay[i] != 100) { if (outsClocksCounts[i] == 0) { //Pulse on 0 digitalWrite(outsPins[i], HIGH); } if (outsClocksCounts[i] < (outsModesPlay[i] - 1)) { outsClocksCounts[i]++; } else { outsClocksCounts[i] = 0; } } else if (outsModesPlay[i] == 100) { //random if (outsClocksCounts[i] == 0) { if (random(2)) { // random(2) gives either 0 or 1, so it's 50/50 chance digitalWrite(outsPins[i], HIGH); } } } } beatCounted = true; } //multiplier for (int i = 0; i<6; i++) { if (outsModesPlay[i] < 0) { if (outsClocksCounts[i] == 0) { //Pulse on 0 digitalWrite(outsPins[i], HIGH); } if (outsClocksCounts[i] < (PPQN / abs(outsModesPlay[i])) - 1) { outsClocksCounts[i]++; } else { outsClocksCounts[i] = 0; } } } pulseCounted = true; } //internal pulse if (pulseClockCount == 0) { pulseCount++; beatCounted = false; 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 updatePeriod() { 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; if (bpm > MAXBPM) { bpm = MAXBPM; } else if (bpm < MINBPM) { bpm = MINBPM; } updatePeriod(); } else { outsModeIndex[displayTab-1] = outsModeIndex[displayTab-1] + change; if (outsModeIndex[displayTab-1] < 0) { outsModeIndex[displayTab-1] = 0; } else if (outsModeIndex[displayTab-1] > (sizeof(outsModes)/sizeof(int)) - 1) { outsModeIndex[displayTab-1] = (sizeof(outsModes)/sizeof(int)) - 1; } 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; } //modulations a1Input = analogRead(ANALOGUE_INPUT_1_PIN); a1Input = map (a1Input, 0, 1023, 0, 4); } 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 (outsModes[outsModeIndex[displayTab-1]] == 0) { display.print(F("OFF")); } else if (outsModes[outsModeIndex[displayTab-1]] == 100) { display.print(F("/RANDOM")); } else if (outsModes[outsModeIndex[displayTab-1]]>0) { display.print(F("/")); display.print(abs(outsModes[outsModeIndex[displayTab-1]])); } else { display.print(F("x")); display.print(abs(outsModes[outsModeIndex[displayTab-1]])); } } display.display(); }