#define CLK 3
#define DT 4
int counter = 0;

void setup() {
  pinMode(CLK, INPUT);
  pinMode(DT, INPUT);
  Serial.begin(9600);
}

void loop() {
 readEncoder();
}

void readEncoder(){
 static int lastStateCLK = digitalRead(CLK);
 int currentStateCLK = digitalRead(CLK);
  if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
    if (digitalRead(DT) != currentStateCLK) {
      counter--;
      if (counter < 0) counter = 0;
    } else {
      counter++;
      if (counter > 20) counter = 20;
    }
    Serial.println(counter);
  } 
  lastStateCLK = currentStateCLK;
}