#include <Arduino.h>
#define CLK_PIN1 25
#define DT_PIN1 26
#define CLK_PIN2 22
#define DT_PIN2 23
#define CLK_PIN3 19
#define DT_PIN3 18
#define CLK_PIN4 16
#define DT_PIN4 17
volatile int counter1 = 0;
volatile int counter2 = 0;
volatile int counter3 = 0;
volatile int counter4 = 0;
void handleInterrupt1() {
if (digitalRead(DT_PIN1) == HIGH) {
counter1--;
} else {
counter1++;
}
}
void handleInterrupt2() {
if (digitalRead(DT_PIN2) == HIGH) {
counter2--;
} else {
counter2++;
}
}
void handleInterrupt3() {
if (digitalRead(DT_PIN3) == HIGH) {
counter3--;
} else {
counter3++;
}
}
void handleInterrupt4() {
if (digitalRead(DT_PIN4) == HIGH) {
counter4--;
} else {
counter4++;
}
}
void setup() {
Serial.begin(115200);
pinMode(CLK_PIN1, INPUT_PULLUP);
pinMode(DT_PIN1, INPUT_PULLUP);
pinMode(CLK_PIN2, INPUT_PULLUP);
pinMode(DT_PIN2, INPUT_PULLUP);
pinMode(CLK_PIN3, INPUT_PULLUP);
pinMode(DT_PIN3, INPUT_PULLUP);
pinMode(CLK_PIN4, INPUT_PULLUP);
pinMode(DT_PIN4, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(CLK_PIN1), handleInterrupt1, RISING);
attachInterrupt(digitalPinToInterrupt(CLK_PIN2), handleInterrupt2, RISING);
attachInterrupt(digitalPinToInterrupt(CLK_PIN3), handleInterrupt3, RISING);
attachInterrupt(digitalPinToInterrupt(CLK_PIN4), handleInterrupt4, RISING);
}
void loop() {
Serial.print("Counter 1: ");
Serial.println(counter1);
Serial.print("Counter 2: ");
Serial.println(counter2);
Serial.print("Counter 3: ");
Serial.println(counter3);
Serial.print("Counter 4: ");
Serial.println(counter4);
delay(1000);
}