diff --git a/Out.cpp b/Out.cpp index c2f8ea4..03b7310 100644 --- a/Out.cpp +++ b/Out.cpp @@ -6,8 +6,11 @@ Out::Out(byte pin) { this->pin = pin; state = LOW; - + + divideMode = 1; + modifier = 1; div = 1; + divString = ""; cycle = 0; dur = 0; @@ -20,8 +23,6 @@ Out::Out(byte pin) { } void Out::setLen(unsigned long currentPeriod) { - // Fix the integer division by using floating-point math temporarily - // The result 'len' is likely a duration in milliseconds, so keep it an integer type len = (unsigned long)((double)currentPeriod * (width / 100.0) / 1000.0); } @@ -40,21 +41,36 @@ void Out::turnOn() { void Out::turnOff() { if (state == HIGH && millis() - dur >= len) { - Serial.println(len); state = LOW; digitalWrite(pin, state); dur = 0; }; } -void Out::setDiv(int newDiv) { - div = newDiv; +void Out::setDiv(int modifier, byte divide = 1) { + if (divide == 1) { + div = ppqn * modifier; + divString = "/" + modifier; + } else { + div = ppqn / modifier; + divString = "x" + modifier; + } + divideMode = divide; + this->modifier = modifier; }; void Out::setWidth(int newWidth) { + Serial.println(modifier); + Serial.println(divideMode); width = newWidth; - len = (unsigned long)((double)period * (width / 100.0) / 1000.0); + if (divideMode == 1) { + len = (unsigned long)((double)(minute / BPM) * (width / 100.0) / 1000.0); + } else { + len = (unsigned long)((double)(minute / BPM / modifier) * (width / 100.0) / 1000.0); + } + Serial.println(len); + Serial.println("========="); }; int Out::getState() { diff --git a/Out.h b/Out.h index 363446e..0df40ff 100644 --- a/Out.h +++ b/Out.h @@ -11,18 +11,20 @@ class Out { int cycle; unsigned long dur; unsigned long len; - + byte width; + byte divideMode; + int div; + int modifier; + String divString; public: Out(byte pin); void turnOn(); void turnOff(); void setLen(unsigned long currentPeriod); - void setDiv(int newDiv); + void setDiv(int newDiv, byte divide = 1); void setWidth(int newWidth); int getState(); - byte width; - int div; }; diff --git a/globals.h b/globals.h index 02df380..5a25180 100644 --- a/globals.h +++ b/globals.h @@ -3,5 +3,49 @@ #include +extern int BPM; +extern unsigned long minute; extern unsigned long period; +extern byte ppqn; #endif // GLOBALS_H + +/* +TODO: + +PRE-DAC: +[ ] Figure out multiplicative beats X2 X4 X8 X16 X32? +[ ] Swing/Phase (same thing) +[ ] Probability +[ ] Humanization +[ ] Euclidian Rhythms + [ ] Steps - # of steps for a full pattern + [ ] Hits - how many hits across the steps, must be less than steps + [ ] Offset - move the starting point of the pattern +[ ] Logic (NO | AND | OR | XOR) +[ ] Mute +[ ] Save +[ ] Load + +POST-DAC: +[ ] Different Wave Forms +[ ] Different Voltage levels +[ ] v/oct? + +100BPM +4800BPM + +POSSIBLE DIVISIONS: +1: x48 +16: x32 +32: x16 +40: x8 +44: x4 +46: x2 +--- +48*1: 1 ** THIS NEEDS TO BE PASSED IN AS DIVIDE MODE +48*2 = 96: /2 +48*4 = 192: /4 +48*8 = /8 +48*16 = /16 + +*/ diff --git a/master_clock.ino b/master_clock.ino index 8bfd94e..14c80dc 100644 --- a/master_clock.ino +++ b/master_clock.ino @@ -3,8 +3,8 @@ #include #include #include -#include "globals.h" +#include "globals.h" #include "Out.h" #define SCREEN_WIDTH 128 @@ -12,21 +12,17 @@ #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C - Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -int BPM = 140; -unsigned long period = 60000000L / BPM; +byte play = 1; +int BPM = 100; + +unsigned long minute = 60000000L; +byte ppqn = 96; +unsigned long period = (minute / BPM) / ppqn; + volatile boolean beatToggle = false; -int clockDivCount = 0; - -int mode = 0; - -String modes[] = { - "CLK DIV" -}; - Out out1(4); Out out2(5); Out out3(6); @@ -38,19 +34,16 @@ Out out8(11); byte btnPin = 13; byte btnState = 0; -byte play = 1; -// the setup function runs once when you press reset or power the board void setup() { pinMode(btnPin, INPUT_PULLUP); Serial.begin(9600); - // init display if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); - for (;;); // Don't proceed, loop forever + for (;;); } display.clearDisplay(); @@ -62,17 +55,14 @@ void setup() { String bpmCalculatedString = bpmString + BPM; display.println(bpmCalculatedString); - // String modeString = "MODE: "; - // String currentModeString = modes[mode]; - // display.println(modeString + currentModeString); - display.display(); - //init timer + // init timer Timer1.initialize(1000000); Timer1.attachInterrupt(clock); Timer1.setPeriod(period); + // init outputs out1.setLen(period); out2.setLen(period); out3.setLen(period); @@ -83,29 +73,29 @@ void setup() { out8.setLen(period); // manual setup - out1.setDiv(1); - out1.setWidth(25); + out1.setDiv(8, 0); + out1.setWidth(50); - out2.setDiv(2); - out2.setWidth(25); + out2.setDiv(4, 0); + out2.setWidth(50); - out3.setDiv(4); - out3.setWidth(25); + out3.setDiv(2, 0); + out3.setWidth(50); - out4.setDiv(8); - out4.setWidth(25); + out4.setDiv(1); + out4.setWidth(50); - out5.setDiv(16); - out5.setWidth(25); + out5.setDiv(2); + out5.setWidth(50); - out6.setDiv(32); - out6.setWidth(25); + out6.setDiv(4); + out6.setWidth(50); - out7.setDiv(64); - out7.setWidth(25); + out7.setDiv(8); + out7.setWidth(50); - out8.setDiv(1); - out8.setWidth(10); + out8.setDiv(16); + out8.setWidth(50); } void clock(void) { @@ -148,4 +138,4 @@ void checkBtn() { }; } btnState = currentBtnState; -}; \ No newline at end of file +};