#include #include #include #include #include #include const char version[5] = "MIDI Test"; #define SCREEN_ADDRESS 0x3C #define PPQN 24 #define PULSE_LENGTH 12 //ms (with 12 ms you can't get higher than 208bpm) #define MAXBPM 200 //250 at 24ppqn with 5ms pulse will be 50/50 square wave #define MINBPM 20 // Rev 2+ Config #define ENC_BTN_PIN 14 #define ENC_D1_PIN 17 #define ENC_D2_PIN 4 #define START_STOP_BTN_PIN 5 #define SHIFT_BTN_PIN 12 #define EXT_INPUT_PIN 2 //needs to be an interrupt pin #define ANALOGUE_INPUT_1_PIN A7 #define ANALOGUE_INPUT_2_PIN A6 const byte outsPins[6] = { 7, 8, 10, 6, 9, 11 }; const byte clockOutPin = 3; bool rotateScreen = false; // /* Rev 1 Config #define ENC_BTN_PIN 14 #define ENC_D1_PIN 17 #define ENC_D2_PIN 4 #define START_STOP_BTN_PIN 5 #define SHIFT_BTN_PIN 100 #define EXT_INPUT_PIN 2 //needs to be an interrupt pin #define ANALOGUE_INPUT_1_PIN A2 #define ANALOGUE_INPUT_2_PIN A1 const byte clockOutPin = 13; const int outsPins[6] = {6, 11, 7, 10, 8, 9}; bool rotateScreen = true; */ int CV1Calibration = 512; int CV2Calibration = 512; bool showDone = false; const int subDivs[20] = { -24, -12, -8, -6, -4, -3, -2, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32, 64, 128 }; //positive - divide, negative - multiply, 0 - off byte bpm = 130; byte bpmModulationChannel = 200; //0 - CV1, 1 - CV2, 255 - OFF byte bpmModulationRange = 0; struct channel { byte mode; //0 - CLK, 1 - RND, 2 - SEQ byte subDiv; byte CV1Target; //0 - Off, 1 - Subdiv, 2 - RND, 3 - SeqPattern byte CV1Range; byte CV2Target; byte CV2Range; unsigned int offset; byte random; byte seqPattern; }; channel channels[6] = { //array of channel settings { 0, 7, 0, 0, 0, 0, 0, 0, 0 }, { 0, 7, 0, 0, 0, 0, 0, 0, 0 }, { 0, 7, 0, 0, 0, 0, 0, 0, 0 }, { 0, 7, 0, 0, 0, 0, 0, 0, 0 }, { 0, 7, 0, 0, 0, 0, 0, 0, 0 }, { 0, 7, 0, 0, 0, 0, 0, 0, 0 } }; bool seqA1[16] = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1}; bool seqA2[16] = {0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}; bool seqA3[16] = {1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0}; bool seqA4[16] = {0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1}; bool seqA5[16] = {0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1}; bool seqA6[16] = {0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0}; bool seqA7[16] = {1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0}; bool seqA8[16] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1}; bool seqB1[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB2[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB3[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB4[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB5[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB6[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB7[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; bool seqB8[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; byte currentStep = 0; byte stepNumSelected = 0; bool *patternToEdit; byte memCode = 'B'; //Change to different letter if you changed the data structure unsigned int channelPulseCount[6]; unsigned int channelPulsesPerCycle[6]; byte sixteenthPulseCount = 0; int playingModes[6]; // should be renamed to currentSubdivs or something. Updated from channels object on beat and with applied CV modulation int playingModesOld[6]; unsigned int pulsePeriod; bool isPlaying = false; bool isRecording = false; bool recordToNextStep = false; bool MIDIClockRecived = false; unsigned int tickCount = 0; unsigned int pulseCount = 0; byte masterClockMode = 0; // 0 - internal, 1 - external 24ppqn, 2 - MIDI byte extClockPPQN = 0; // 0 - 24, 1 - 4 (1/16) unsigned long lastExtPulseTime; unsigned long newExtPulseTime; bool needPulseReset[6] = { true, true, true, true, true, true }; byte displayTab = 0; bool insideTab = false; byte menuItem = 0; bool menuItemSelected = false; byte lastMenuItem = 3; byte displayScreen = 0; //0 - main, 1 - sequencer, 2 - settings bool playBtnPushed = false; bool shiftBtnPushed = false; int CV1Input = 0; int CV2Input = 0; int encPositionOld = 0; unsigned long encPressedTime; unsigned long encReleasedTime; unsigned long playPressedTime; unsigned long playReleasedTime; unsigned long shiftPressedTime; unsigned long shiftReleasedTime; bool encBtnPushed; int extResetCountdown; int extTriggerCount; //unsigned long lastInteractionTime; // used for display timeout U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE); RotaryEncoder encoder(ENC_D1_PIN, ENC_D2_PIN, RotaryEncoder::LatchMode::TWO03); //Font const uint8_t velvetscreen[437] U8G2_FONT_SECTION("velvetscreen") = "\64\0\2\2\3\3\2\3\4\5\5\0\0\5\0\5\0\0\221\0\0\1\230 \4\200\134%\11\255tT" "R\271RI(\6\252\334T\31)\7\252\134bJ\12+\7\233\345\322J\0,\5\221T\4-\5\213" "f\6.\5\211T\2/\6\244\354c\33\60\10\254\354T\64\223\2\61\7\353\354\222\254\6\62\11\254l" "\66J*\217\0\63\11\254l\66J\32\215\4\64\10\254l\242\34\272\0\65\11\254l\206\336h$\0\66" "\11\254\354T^\61)\0\67\10\254lF\216u\4\70\11\254\354TL*&\5\71\11\254\354TL;" ")\0:\6\231UR\0A\10\254\354T\34S\6B\11\254lV\34)\216\4C\11\254\354T\324\61" ")\0D\10\254lV\64G\2E\10\254l\206\36z\4F\10\254l\206^\71\3G\11\254\354TN" "\63)\0H\10\254l\242\34S\6I\6\251T\206\0J\10\254\354k\231\24\0K\11\254l\242J\62" "\225\1L\7\254lr{\4M\11\255t\362ZI\353\0N\11\255t\362TI\356\0O\10\254\354T" "\64\223\2P\11\254lV\34)g\0Q\10\254\354T\264b\12R\10\254lV\34\251\31S\11\254\354" "FF\32\215\4T\7\253dVl\1U\10\254l\242\63)\0V\11\255t\262Ne\312\21W\12\255" "t\262J*\251.\0X\11\254l\242L*\312\0Y\12\255tr\252\63\312(\2Z\7\253df*" "\7p\10\255\364V\266\323\2q\7\255\364\216\257\5r\10\253d\242\32*\2t\6\255t\376#w\11" "\255\364V\245FN\13x\6\233dR\7\0\0\0\4\377\377\0"; const uint8_t fabryka[605] U8G2_FONT_SECTION("fabryka") = "\25\0\4\4\5\5\2\1\6\20\30\1\0\30\0\0\0\1K\0\0\2@%'\17>\226\261\245FL" "\64B\214\30\22\223\220)Bj\10Q\232\214\42R\206\310\210\21d\304\30\32a\254\304\270!\0/\15" "\356=\336II\322\177:v,\0\60$\16\77\36TjX\20;h\273c\250D\30\22bF\214\21" "A&D!;g\320\206\307H\260Q\205\6\0\61\27\16\77.\222\5\315\35#\61\212\310\250\61\243\4" "\215\235\377g-:\62\33\16\77\36TjX\20;hp\354L\311\235\321\6UI\242c\347\366\301\3" "\6\63\35\16\77\36TjX\20;hp\354P\222\264\264\226\354l\15\32 0) { if (Serial.read() == 0xF8) { //Clock MIDIClockRecived = true; //} else if (Serial.read() == 0xFA || Serial.read() == 0xFB) { //start and continue isPlaying = true; //} else if (Serial.read() == 0xFC) { //stop // isPlaying = false; } } } void clock() { if (isPlaying) { // Action on each pulse if (tickCount == 0) { sendTriggers(); digitalWrite(clockOutPin, HIGH); sendMIDIClock(); } //this part gets the Pulse and Ticks ticking //it's placed after the triggers to avoid problems on the start (when pulseCount==0) tickCount++; if (masterClockMode == 0) { if (tickCount >= pulsePeriod) { tickCount = 0; if (pulseCount < (PPQN - 1)) { //-1 is here to avoid extra IF to reset to 0 pulseCount++; } else { pulseCount = 0; } if (bpmModulationRange != 0) { calculateBPMTiming(); } } } if (masterClockMode == 1 && extClockPPQN == 1) { if (tickCount >= pulsePeriod && pulseCount < (PPQN - 1)) { // ((6 * (extTriggerCount + 1)) - 1)) { //this formula puts it out of sync, so there's PPQN-1 for now tickCount = 0; pulseCount++; } } if (masterClockMode == 2 && MIDIClockRecived) { // MIDI should happen here (needs testing) tickCount = 0; //to make things happen in the main clock function if (pulseCount < (PPQN - 1)) { pulseCount++; } else { pulseCount = 0; } MIDIClockRecived = false; } // pull low all outputs after set pulse length if (tickCount >= PULSE_LENGTH) { for (byte i = 0; i < 6; i++) { digitalWrite(outsPins[i], LOW); } digitalWrite(clockOutPin, LOW); } } } void externalClock() { lastExtPulseTime = newExtPulseTime; newExtPulseTime = millis(); if (masterClockMode == 1 && extClockPPQN == 0) { // EXT-24 //reset cycles if there was no pulses for a while if ((newExtPulseTime - lastExtPulseTime) > 125) { //125ms is 20bpm for (byte i = 0; i < 6; i++) { channelPulseCount[i] = 0; } } if (!isPlaying) { isPlaying = true; } tickCount = 0; //to make things happen in the main clock function if (pulseCount < (PPQN - 1)) { pulseCount++; } else { pulseCount = 0; } } else if (masterClockMode == 1 && extClockPPQN == 1) { //EXT 1/16 if (!isPlaying) { isPlaying = true; } if ((newExtPulseTime - lastExtPulseTime) > 750) { resetClocks(); extResetCountdown = 0; extTriggerCount = 0; } if (extTriggerCount == 0) { //happens on beat pulseCount = 0; tickCount = 0; } if (extTriggerCount < 3) { extTriggerCount++; } else { extTriggerCount = 0; } if (extResetCountdown < 4) { //reset on the second beat (5th pulse), so that BPM is already calculated correctly extResetCountdown++; } else if (extResetCountdown == 4) { resetClocks(); extResetCountdown++; //to get out of the loop } } } void sendTriggers() { for (byte i = 0; i < 6; i++) { if (playingModes[i] != subDivs[channels[i].subDiv] && playingModesOld[i] != playingModes[i]) { needPulseReset[i] = true; playingModesOld[i] = playingModes[i]; } } //16th notes for sequencer if (sixteenthPulseCount == 0) { bool *currentSeq; for (byte i = 0; i < 6; i++) { //pattern modulation int seqMod = 0; byte seqPattern; if (channels[i].CV2Target == 3) { seqMod = map(CV2Input, -1, 1024, -8, 8); //-1 and 1024 are to try to make the last step not at max value (should make the range from -7 to +7) } else if (channels[i].CV1Target == 3) { seqMod = map(CV1Input, -1, 1024, -8, 8); } if (channels[i].seqPattern < 8 && channels[i].seqPattern + seqMod >= 8) { seqPattern = 7; } else if (channels[i].seqPattern < 8 && channels[i].seqPattern + seqMod < 0) { seqPattern = 0; } else if (channels[i].seqPattern >= 8 && channels[i].seqPattern + seqMod < 8) { seqPattern = 8; } else if (channels[i].seqPattern >= 8 && channels[i].seqPattern + seqMod >= 16) { seqPattern = 15; } else { seqPattern = channels[i].seqPattern + seqMod; } if (seqPattern == 0) { currentSeq = seqA1; } else if (seqPattern == 1) { currentSeq = seqA2; } else if (seqPattern == 2) { currentSeq = seqA3; } else if (seqPattern == 3) { currentSeq = seqA4; } else if (seqPattern == 4) { currentSeq = seqA5; } else if (seqPattern == 5) { currentSeq = seqA6; } else if (seqPattern == 6) { currentSeq = seqA7; } else if (seqPattern == 7) { currentSeq = seqA8; } else if (seqPattern == 8) { currentSeq = seqB1; } else if (seqPattern == 9) { currentSeq = seqB2; } else if (seqPattern== 10) { currentSeq = seqB3; } else if (seqPattern == 11) { currentSeq = seqB4; } else if (seqPattern == 12) { currentSeq = seqB5; } else if (seqPattern == 13) { currentSeq = seqB6; } else if (seqPattern == 14) { currentSeq = seqB7; } else if (seqPattern == 15) { currentSeq = seqB8; } if (channels[i].mode == 2 && channelPulseCount[i] == 0 && currentSeq[currentStep]) { digitalWrite(outsPins[i], HIGH); } } } if (sixteenthPulseCount < (PPQN / 4) - 1) { sixteenthPulseCount++; if (sixteenthPulseCount > 3) { //quantization. might need fine-tuning recordToNextStep = true; } } else { sixteenthPulseCount = 0; if (currentStep < 15) { currentStep ++; } else { currentStep = 0; } recordToNextStep = false; } //switching modes on the beat and resetting channel clock if (pulseCount == 0) { calculateCycles(); for (byte i = 0; i < 6; i++) { if (needPulseReset[i] == true) { channelPulseCount[i] = 0; needPulseReset[i] = false; } } } //multiplier for (byte i = 0; i < 6; i++) { //RND modulation byte randMod = 0; if (channels[i].CV1Target == 2) { randMod = randMod + CV1Input; } if (channels[i].CV2Target == 2) { randMod = randMod + CV2Input; } if (channels[i].CV1Target == 2 || channels[i].CV2Target == 2) { randMod = map(randMod, 0, 1023, -5, +5); } byte randAmount = channels[i].random + randMod; if (randAmount > 100) { randAmount = 0; } else if (randAmount > 10) { randAmount = 10; } if ((channels[i].mode == 0 && channelPulseCount[i] == channels[i].offset) //CLK with offset || (channels[i].mode == 1 && channelPulseCount[i] == 0 && (random(10) + 1) > randAmount) //RND ) { digitalWrite(outsPins[i], HIGH); } if (channelPulseCount[i] < channelPulsesPerCycle[i]) { channelPulseCount[i]++; } else { channelPulseCount[i] = 0; } } } void calculateCycles() { for (byte i = 0; i < 6; i++) { int mod = 0; //subdiv modulation happens here if (channels[i].CV1Target == 1) { mod = map(CV1Input, -1, 1024, -5, 5); //(channels[i].CV1Value * -1), channels[i].CV1Value) } else if (channels[i].CV2Target == 1) { mod = map(CV2Input, -1, 1024, -5, 5); } playingModes[i] = subDivs[channels[i].subDiv - mod]; //subtracting because the innitial array is backwards if (playingModes[i] > 0 && channels[i].mode != 2) { channelPulsesPerCycle[i] = (playingModes[i] * PPQN) - 1; } else if (playingModes[i] <= 0 && channels[i].mode != 2) { channelPulsesPerCycle[i] = (PPQN / abs(playingModes[i])) - 1; } else if (channels[i].mode == 2) { //Sequencer plays 1/16th channelPulsesPerCycle[i] = (PPQN / 4) - 1; } } } void calculateBPMTiming() { int mod = 0; if (masterClockMode == 0) { //Internal clock if (bpmModulationRange != 0 && bpmModulationChannel == 0) { mod = map(CV1Input, 0, 1023, bpmModulationRange * -10, bpmModulationRange * 10); } else if (bpmModulationRange != 0 && bpmModulationChannel == 1) { mod = map(CV2Input, 0, 1023, bpmModulationRange * -10, bpmModulationRange * 10); } pulsePeriod = 60000 / ((bpm + mod) * PPQN); } else if (masterClockMode == 1 && extClockPPQN == 1) { //for ext 1/16 clock (hardcoded) pulsePeriod = (newExtPulseTime - lastExtPulseTime) / 6; } } void resetClocks() { for (byte i = 0; i < 6; i++) { channelPulseCount[i] = 0; digitalWrite(outsPins[i], LOW); //to avoid stuck leds } pulseCount = 0; tickCount = 0; sixteenthPulseCount = 0; currentStep = 0; } void saveState() { int addr = 0; EEPROM.put(addr, bpm); addr = addr + sizeof(bpm); EEPROM.put(addr, bpmModulationChannel); addr = addr + sizeof(bpmModulationChannel); EEPROM.put(addr, bpmModulationRange); addr = addr + sizeof(bpmModulationRange); EEPROM.put(addr, masterClockMode); addr = addr + sizeof(masterClockMode); EEPROM.put(addr, channels); addr = addr + sizeof(channels); EEPROM.put(addr, seqA1); addr = addr + sizeof(seqA1); EEPROM.put(addr, seqA2); addr = addr + sizeof(seqA2); EEPROM.put(addr, seqA3); addr = addr + sizeof(seqA3); EEPROM.put(addr, seqA4); addr = addr + sizeof(seqA4); EEPROM.put(addr, seqA5); addr = addr + sizeof(seqA5); EEPROM.put(addr, seqA6); addr = addr + sizeof(seqA6); EEPROM.put(addr, seqA7); addr = addr + sizeof(seqA7); EEPROM.put(addr, seqA8); addr = addr + sizeof(seqA8); EEPROM.put(addr, seqB1); addr = addr + sizeof(seqB1); EEPROM.put(addr, seqB2); addr = addr + sizeof(seqB2); EEPROM.put(addr, seqB3); addr = addr + sizeof(seqB3); EEPROM.put(addr, seqB4); addr = addr + sizeof(seqB4); EEPROM.put(addr, seqB5); addr = addr + sizeof(seqB5); EEPROM.put(addr, seqB6); addr = addr + sizeof(seqB6); EEPROM.put(addr, seqB7); addr = addr + sizeof(seqB7); EEPROM.put(addr, seqB8); addr = addr + sizeof(seqB8); EEPROM.put(addr, CV1Calibration); addr = addr + sizeof(CV1Calibration); EEPROM.put(addr, CV2Calibration); addr = addr + sizeof(CV2Calibration); EEPROM.put(addr, rotateScreen); } void loadState() { //check last bit in eeprom to know if the correct settings were stored if (EEPROM.read(1023) == memCode) { int addr = 0; EEPROM.get(addr, bpm); addr = addr + sizeof(bpm); EEPROM.get(addr, bpmModulationChannel); addr = addr + sizeof(bpmModulationChannel); EEPROM.get(addr, bpmModulationRange); addr = addr + sizeof(bpmModulationRange); EEPROM.get(addr, masterClockMode); addr = addr + sizeof(masterClockMode); EEPROM.get(addr, channels); addr = addr + sizeof(channels); EEPROM.get(addr, seqA1); addr = addr + sizeof(seqA1); EEPROM.get(addr, seqA2); addr = addr + sizeof(seqA2); EEPROM.get(addr, seqA3); addr = addr + sizeof(seqA3); EEPROM.get(addr, seqA4); addr = addr + sizeof(seqA4); EEPROM.get(addr, seqA5); addr = addr + sizeof(seqA5); EEPROM.get(addr, seqA6); addr = addr + sizeof(seqA6); EEPROM.get(addr, seqA7); addr = addr + sizeof(seqA7); EEPROM.get(addr, seqA8); addr = addr + sizeof(seqA8); EEPROM.get(addr, seqB1); addr = addr + sizeof(seqB1); EEPROM.get(addr, seqB2); addr = addr + sizeof(seqB2); EEPROM.get(addr, seqB3); addr = addr + sizeof(seqB3); EEPROM.get(addr, seqB4); addr = addr + sizeof(seqB4); EEPROM.get(addr, seqB5); addr = addr + sizeof(seqB5); EEPROM.get(addr, seqB6); addr = addr + sizeof(seqB6); EEPROM.get(addr, seqB7); addr = addr + sizeof(seqB7); EEPROM.get(addr, seqB8); addr = addr + sizeof(seqB8); EEPROM.get(addr, CV1Calibration); addr = addr + sizeof(CV1Calibration); EEPROM.get(addr, CV2Calibration); addr = addr + sizeof(CV2Calibration); EEPROM.get(addr, rotateScreen); } else { //calibrateCVs(); saveState(); EEPROM.write(1023, memCode); } } void reboot() { wdt_enable(WDTO_15MS); //reboot after 15ms while(true); } void calibrateCVs() { CV1Calibration = analogRead(ANALOGUE_INPUT_1_PIN); CV2Calibration = analogRead(ANALOGUE_INPUT_2_PIN); showDone = true; updateScreen(); } void checkScreenRotation() { if (rotateScreen) { u8g2.setDisplayRotation(U8G2_R0); } else { u8g2.setDisplayRotation(U8G2_R2); } }