#include <Encoder.h>
// Define the pins for the rotary encoder
int CLK = 2; // Connected to CLK pin on rotary encoder
int DT = 3; // Connected to DT pin on rotary encoder
int SW = 4; // Connected to SW pin on rotary encoder
// Initialize the rotary encoder
Encoder myEnc(CLK, DT);
// Keep track of the old position
long oldPosition = -999;
void setup() {
pinMode(SW, INPUT);
digitalWrite(SW, HIGH); // Pull-up resistor for stable readings
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
//digitalWrite(10, LOW); // Ensure the LED is off when the program starts
digitalWrite(11, LOW); // Ensure the LED is off when the program starts
digitalWrite(12, LOW);
}
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
if (newPosition > oldPosition) {
digitalWrite(10, HIGH);
delay(25);
digitalWrite(10, LOW);
}
else {
digitalWrite(11, HIGH);
delay(25);
digitalWrite(11, LOW);
}
}
if (digitalRead(SW) == LOW) {
digitalWrite(12, HIGH);
delay(25);
digitalWrite(12, LOW);
}
}