#define clk 32 //Pin A
#define dt 33 //Pin B
#define sw 25 //Push Button
long counter = 0;
void setup() {
Serial.begin(115200);
pinMode(clk, INPUT_PULLUP);
pinMode(dt, INPUT_PULLUP);
pinMode(sw, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(clk), read_encoder, FALLING);
}
void read_encoder() {
double dt_val = digitalRead(dt);
if (dt_val == 1) counter++;
if (dt_val == 0) counter--;
}
long Counter() {
int res;
noInterrupts();
res = counter;
interrupts();
return res;
}
void reset() {
noInterrupts();
counter = 0;
interrupts();
}
void loop() {
Serial.print("Counter = ");
Serial.println(Counter());
if (!digitalRead(sw) == 1) reset();
}