#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display TWI/I2C address (usually 0x3C or 0x3D)
#define OLED_ADDR 0x3C
// OLED display width and height, in pixels
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// LED OUTPUT SHIFT REGISTER SETUP
// Define the connections to the shift register
const int dataPin = 23; // SER (Serial Input)
const int latchPin = 19; // RCLK (Register Clock)
const int clockPin = 18; // SRCLK (Shift Register Clock)
// Declaration for an SSD1306 display connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
// Encoder Pin Definitions
const int encoderClk = 25;
const int encoderDt = 33;
// Variables to hold the encoder state
volatile int encoderPos = 0;
int lastEncoded = 0;
bool encoderUpdated = false;
void setup() {
Serial.begin(115200);
// initialize with the I2C addr 0x3C (for the 128x64)
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Encoder setup
pinMode(encoderClk, INPUT);
pinMode(encoderDt, INPUT);
attachInterrupt(digitalPinToInterrupt(encoderClk), updateEncoder, CHANGE);
attachInterrupt(digitalPinToInterrupt(encoderDt), updateEncoder, CHANGE);
// Clear the buffer
display.clearDisplay();
// Set text size, color, and position
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
// Display static text
display.println(F("Hello, ESP32!"));
display.println(F("SSD1306 OLED Test"));
// Update the display with all of the above graphics
display.display();
// LED SHIFT REGISTER
// Set pins to output
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Serial.println("hello bitch");
}
void updateEncoder() {
int MSB = digitalRead(encoderClk); //MSB = most significant bit
int LSB = digitalRead(encoderDt); //LSB = least significant bit
int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderPos++;
if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderPos--;
lastEncoded = encoded; //store this value for next time
// Indicate that the encoder position has been updated
encoderUpdated = true;
}
void updateShiftRegister(byte data) {
// Take the latchPin low so the LEDs don't change while sending in bits
digitalWrite(latchPin, LOW);
// Shift out the bits
shiftOut(dataPin, clockPin, MSBFIRST, data);
// Take the latchPin high so the LEDs will light up
digitalWrite(latchPin, HIGH);
}
void loop() {
if(encoderUpdated) {
// Update the display
display.clearDisplay();
display.setCursor(0, 0);
display.print("Encoder Pos: ");
display.println(encoderPos);
display.display();
// Reset the flag
encoderUpdated = false;
}
// LED SHIFT REGISTER
// Turn all LEDs on
updateShiftRegister(B11111111);
delay(1000); // Wait for 1 second
// Turn all LEDs off
updateShiftRegister(B00000000);
delay(1000); // Wait for 1 second
}