actually working encoder

This commit is contained in:
Dominic DiTaranto 2026-01-19 18:33:26 -05:00
parent 9925d68aa0
commit 69e8443063
3 changed files with 45 additions and 5 deletions

View file

@ -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) {

View file

@ -3,7 +3,7 @@
#include <Arduino.h>
extern int BPM;
extern byte BPM;
extern unsigned long minute;
extern unsigned long period;
extern byte ppqn;

View file

@ -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
}
}