#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#include <ESP32Encoder.h>
#include <BluetoothSerial.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMPIXELS 16 // Number of pixels in WS2812 strip
#define LED_PIN 21 // Pin connected to the WS2812 data line
Adafruit_NeoPixel pixels(NUMPIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
ESP32Encoder encoder1;
ESP32Encoder encoder2;
BluetoothSerial SerialBT;
// Define push buttons pins
const int buttonPins[10] = {2, 4, 5, 13, 14, 15, 18, 19, 22, 23};
// Variables to store encoder positions
int32_t lastPosition1 = 0;
int32_t lastPosition2 = 0;
void setup() {
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
// Initialize WS2812
pixels.begin();
pixels.clear();
pixels.show();
// Initialize encoders
ESP32Encoder::useInternalWeakPullResistors = UP;
encoder1.attachHalfQuad(32, 33); // Pins for the first encoder
encoder2.attachHalfQuad(25, 26); // Pins for the second encoder
// Initialize push buttons
for (int i = 0; i < 10; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
// Initialize Bluetooth
SerialBT.begin("ESP32_PC_Controller");
// Display a welcome message on the OLED
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Wireless PC Controller");
display.display();
delay(2000);
}
void loop() {
// Read encoder positions
int32_t newPosition1 = encoder1.getCount();
int32_t newPosition2 = encoder2.getCount();
// Update display with encoder values
if (newPosition1 != lastPosition1 || newPosition2 != lastPosition2) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Encoder 1: ");
display.println(newPosition1);
display.print("Encoder 2: ");
display.println(newPosition2);
display.display();
lastPosition1 = newPosition1;
lastPosition2 = newPosition2;
}
// Check button states and send Bluetooth commands
for (int i = 0; i < 10; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
SerialBT.print("Button ");
SerialBT.print(i + 1);
SerialBT.println(" pressed");
delay(200); // Debounce delay
}
}
// WS2812 pixel light effect (e.g., simple color wipe)
for(int i=0; i<NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 150, 0)); // Green color
pixels.show();
delay(50);
}
pixels.clear();
pixels.show();
}