#include <Adafruit_NeoPixel.h>
const int clkPin = 2; // Pin connected to CLK on the rotary encoder
const int dtPin = 3; // Pin connected to DT on the rotary encoder
const int swPin = 4; // Pin connected to SW on the rotary encoder
const int ledPin = 6; // Pin connected to DIN on the LED ring
const int numLEDs = 16; // Number of LEDs in the ring (change as needed)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLEDs, ledPin, NEO_GRB + NEO_KHZ800);
int lastStateCLK;
int currentStateCLK;
int level = 0;
void setup() {
// Initialize the LED ring
strip.begin();
strip.show(); // Initialize all LEDs to off
// Initialize rotary encoder pins
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT_PULLUP);
// Read the initial state of the CLK pin
lastStateCLK = digitalRead(clkPin);
}
void loop() {
// Read the current state of the CLK pin
currentStateCLK = digitalRead(clkPin);
// If the state has changed, then the encoder has been rotated
if (currentStateCLK != lastStateCLK) {
// Read the DT pin
int dtState = digitalRead(dtPin);
// Determine the direction of rotation
if (dtState != currentStateCLK) {
// Turning clockwise
level++;
if (level > numLEDs) level = numLEDs;
} else {
// Turning counterclockwise
level--;
if (level < 0) level = 0;
}
// Update the LED ring
strip.clear();
for (int i = 0; i < level; i++) {
strip.setPixelColor(i, strip.Color(255, 255, 255)); // Set color to red (you can change the color)
}
strip.show();
}
// Update the last states
lastStateCLK = currentStateCLK;
}