#define CLK 2
#define DT 3
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir = "";
void setup() {
// put your setup code here, to run once:
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
Serial.begin(9600);
lastStateCLK = digitalRead(CLK);
attachInterrupt(digitalPinToInterrupt(2),updateEncoder,CHANGE);
attachInterrupt(digitalPinToInterrupt(3),updateEncoder,CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
}
void updateEncoder(){
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
if(digitalRead(DT) != currentStateCLK){
counter ++;
currentDir = "CW";
}else{
counter --;
currentDir = "CCW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
lastStateCLK = currentStateCLK;
}