encoder can control screen

This commit is contained in:
Dominic DiTaranto 2026-01-19 18:49:15 -05:00
parent 69e8443063
commit e317595cdd

View file

@ -1,7 +1,6 @@
#include <SPI.h> #include <SPI.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_SSD1306.h> #include <U8g2lib.h>
#include <Adafruit_GFX.h>
#include <TimerOne.h> #include <TimerOne.h>
#include "globals.h" #include "globals.h"
@ -14,7 +13,8 @@
#define CLK 2 #define CLK 2
#define DT 3 #define DT 3
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
byte play = 1; byte play = 1;
byte BPM = 100; byte BPM = 100;
@ -41,6 +41,10 @@ volatile long rPotPos = 0;
unsigned long rPotLastUpdate = 0; unsigned long rPotLastUpdate = 0;
const unsigned long rPotThrottle = 50; const unsigned long rPotThrottle = 50;
unsigned long screenLastUpdate = 0;
const unsigned long screenThrottle = 200;
byte forceScreenUpdate = 1;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
@ -52,21 +56,19 @@ void setup() {
pinMode(DT, INPUT_PULLUP); pinMode(DT, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLK), updateEncoder, FALLING); attachInterrupt(digitalPinToInterrupt(CLK), updateEncoder, FALLING);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // screen setup
Serial.println(F("SSD1306 allocation failed")); u8g2.begin();
for (;;);
}
display.clearDisplay(); // display.clearDisplay();
display.setTextSize(2); // display.setTextSize(2);
display.setTextColor(WHITE); // display.setTextColor(WHITE);
display.setCursor(0, 0); // display.setCursor(0, 0);
//
String bpmString = "BPM: "; // String bpmString = "BPM: ";
String bpmCalculatedString = bpmString + BPM; // String bpmCalculatedString = bpmString + BPM;
display.println(bpmCalculatedString); // display.println(bpmCalculatedString);
//
display.display(); // display.display();
// init timer // init timer
Timer1.initialize(1000000); Timer1.initialize(1000000);
@ -140,6 +142,26 @@ void loop() {
checkBtn(); checkBtn();
checkRPot(); checkRPot();
updateScreen();
}
void updateScreen() {
unsigned long currentTime = millis();
if (currentTime - screenLastUpdate >= screenThrottle && forceScreenUpdate == 1) {
screenLastUpdate = currentTime;
forceScreenUpdate = 0;
// The firstPage()/nextPage() loop manages where the drawing commands go
u8g2.firstPage();
do {
// --- Drawing Commands for the CURRENT Page ---
// All drawing commands must live INSIDE this do/while loop.
u8g2.setFont(u8g2_font_helvR08_tf);
u8g2.setCursor(0, 10);
u8g2.print(F("BPM: "));
u8g2.print(BPM); // Example of dynamic data
} while (u8g2.nextPage());
}
} }
void checkBtn() { void checkBtn() {
@ -164,6 +186,15 @@ void checkRPot() {
if (currentPos != previousPos) { if (currentPos != previousPos) {
Serial.print("Encoder Position: "); Serial.print("Encoder Position: ");
Serial.println(currentPos); Serial.println(currentPos);
if (currentPos > previousPos) {
BPM++;
} else {
BPM--;
}
forceScreenUpdate = 1;
previousPos = currentPos; previousPos = currentPos;
} }