diff --git a/Out.cpp b/Out.cpp index f32f9b4..33beeb2 100644 --- a/Out.cpp +++ b/Out.cpp @@ -61,7 +61,6 @@ void Out::turnOn() { byte pRes = 1; if (cycle == div) { - Serial.println(p); if (p < 100) { long r = random(1, 101); if (r > p) { diff --git a/globals.h b/globals.h index b83e73e..d2ea81a 100644 --- a/globals.h +++ b/globals.h @@ -3,7 +3,7 @@ #include -extern int BPM; +extern byte BPM; extern unsigned long minute; extern unsigned long period; extern byte ppqn; diff --git a/master_clock.ino b/master_clock.ino index 14c80dc..21b0913 100644 --- a/master_clock.ino +++ b/master_clock.ino @@ -11,11 +11,13 @@ #define SCREEN_HEIGHT 64 #define OLED_RESET -1 #define SCREEN_ADDRESS 0x3C +#define CLK 2 +#define DT 3 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); byte play = 1; -int BPM = 100; +byte BPM = 100; unsigned long minute = 60000000L; byte ppqn = 96; @@ -35,12 +37,21 @@ Out out8(11); byte btnPin = 13; byte btnState = 0; +volatile long rPotPos = 0; +unsigned long rPotLastUpdate = 0; +const unsigned long rPotThrottle = 50; + void setup() { - pinMode(btnPin, INPUT_PULLUP); - Serial.begin(9600); + pinMode(btnPin, INPUT_PULLUP); + + // rotary encoder set up + pinMode(CLK, INPUT_PULLUP); + pinMode(DT, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(CLK), updateEncoder, FALLING); + if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); @@ -128,6 +139,7 @@ void loop() { out8.turnOff(); checkBtn(); + checkRPot(); } void checkBtn() { @@ -139,3 +151,32 @@ void checkBtn() { } btnState = currentBtnState; }; + +void checkRPot() { + if (millis() - rPotLastUpdate >= rPotThrottle) { + + noInterrupts(); + long currentPos = rPotPos; + interrupts(); + + // Print position only if it has changed + static long previousPos = -999; + if (currentPos != previousPos) { + Serial.print("Encoder Position: "); + Serial.println(currentPos); + previousPos = currentPos; + } + + rPotLastUpdate = millis(); // Reset the timer for throttling the output + } +} + +void updateEncoder() { + // The ISR should be as short and fast as possible + // Check the state of the DT pin to determine direction + if (digitalRead(3) == LOW) { + rPotPos++; // Clockwise + } else { + rPotPos--; // Counter-clockwise + } +}