actually working encoder
This commit is contained in:
parent
9925d68aa0
commit
69e8443063
3 changed files with 45 additions and 5 deletions
1
Out.cpp
1
Out.cpp
|
|
@ -61,7 +61,6 @@ void Out::turnOn() {
|
||||||
byte pRes = 1;
|
byte pRes = 1;
|
||||||
|
|
||||||
if (cycle == div) {
|
if (cycle == div) {
|
||||||
Serial.println(p);
|
|
||||||
if (p < 100) {
|
if (p < 100) {
|
||||||
long r = random(1, 101);
|
long r = random(1, 101);
|
||||||
if (r > p) {
|
if (r > p) {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
extern int BPM;
|
extern byte BPM;
|
||||||
extern unsigned long minute;
|
extern unsigned long minute;
|
||||||
extern unsigned long period;
|
extern unsigned long period;
|
||||||
extern byte ppqn;
|
extern byte ppqn;
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,13 @@
|
||||||
#define SCREEN_HEIGHT 64
|
#define SCREEN_HEIGHT 64
|
||||||
#define OLED_RESET -1
|
#define OLED_RESET -1
|
||||||
#define SCREEN_ADDRESS 0x3C
|
#define SCREEN_ADDRESS 0x3C
|
||||||
|
#define CLK 2
|
||||||
|
#define DT 3
|
||||||
|
|
||||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||||
|
|
||||||
byte play = 1;
|
byte play = 1;
|
||||||
int BPM = 100;
|
byte BPM = 100;
|
||||||
|
|
||||||
unsigned long minute = 60000000L;
|
unsigned long minute = 60000000L;
|
||||||
byte ppqn = 96;
|
byte ppqn = 96;
|
||||||
|
|
@ -35,12 +37,21 @@ Out out8(11);
|
||||||
byte btnPin = 13;
|
byte btnPin = 13;
|
||||||
byte btnState = 0;
|
byte btnState = 0;
|
||||||
|
|
||||||
|
volatile long rPotPos = 0;
|
||||||
|
unsigned long rPotLastUpdate = 0;
|
||||||
|
const unsigned long rPotThrottle = 50;
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(btnPin, INPUT_PULLUP);
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
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)) {
|
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
|
||||||
Serial.println(F("SSD1306 allocation failed"));
|
Serial.println(F("SSD1306 allocation failed"));
|
||||||
for (;;);
|
for (;;);
|
||||||
|
|
@ -128,6 +139,7 @@ void loop() {
|
||||||
out8.turnOff();
|
out8.turnOff();
|
||||||
|
|
||||||
checkBtn();
|
checkBtn();
|
||||||
|
checkRPot();
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkBtn() {
|
void checkBtn() {
|
||||||
|
|
@ -139,3 +151,32 @@ void checkBtn() {
|
||||||
}
|
}
|
||||||
btnState = currentBtnState;
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue