// Define the pins for the first rotary encoder
#define CLK1 2
#define DT1 3
#define SW1 4
// Define the pins for the second rotary encoder
#define CLK2 5
#define DT2 6
#define SW2 7
// Define the pins for the third rotary encoder
#define CLK3 8
#define DT3 9
#define SW3 10
// Counters for each rotary encoder
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
// Last states for each rotary encoder's CLK pin
int lastStateCLK1;
int lastStateCLK2;
int lastStateCLK3;
void setup() {
// Set encoder pins as inputs
pinMode(CLK1, INPUT);
pinMode(DT1, INPUT);
pinMode(SW1, INPUT_PULLUP);
pinMode(CLK2, INPUT);
pinMode(DT2, INPUT);
pinMode(SW2, INPUT_PULLUP);
pinMode(CLK3, INPUT);
pinMode(DT3, INPUT);
pinMode(SW3, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of each CLK
lastStateCLK1 = digitalRead(CLK1);
lastStateCLK2 = digitalRead(CLK2);
lastStateCLK3 = digitalRead(CLK3);
}
void loop() {
// Read the current state of each CLK
int currentStateCLK1 = digitalRead(CLK1);
int currentStateCLK2 = digitalRead(CLK2);
int currentStateCLK3 = digitalRead(CLK3);
// Encoder 1
if (currentStateCLK1 != lastStateCLK1) {
if (digitalRead(DT1) != currentStateCLK1) {
counter1++;
Serial.println("1");
} else {
counter1--;
Serial.println("255");
}
}
lastStateCLK1 = currentStateCLK1;
// Encoder 2
if (currentStateCLK2 != lastStateCLK2) {
if (digitalRead(DT2) != currentStateCLK2) {
counter2++;
Serial.println("1");
} else {
counter2--;
Serial.println("255");
}
}
lastStateCLK2 = currentStateCLK2;
// Encoder 3
if (currentStateCLK3 != lastStateCLK3) {
if (digitalRead(DT3) != currentStateCLK3) {
counter3++;
Serial.println("1");
} else {
counter3--;
Serial.println("255");
}
}
lastStateCLK3 = currentStateCLK3;
// Button presses (if needed)
if (digitalRead(SW1) == LOW) {
// Perform some action for button 1
Serial.println("Button 1 pressed");
delay(200); // Debounce delay
}
if (digitalRead(SW2) == LOW) {
// Perform some action for button 2
Serial.println("Button 2 pressed");
delay(200); // Debounce delay
}
if (digitalRead(SW3) == LOW) {
// Perform some action for button 3
Serial.println("Button 3 pressed");
delay(200); // Debounce delay
}
}