// Incremental encoder KY-040

int CLK = 3 ;
int DT = 4  ;
int SW = 5  ;

int count = 0; 
int currentCLK;
int previousCLK; 

void setup() {   
  pinMode (CLK,INPUT);
  pinMode (DT,INPUT);
  pinMode (SW, INPUT_PULLUP) ;
  Serial.begin (9600);
  previousCLK = digitalRead(CLK);
} 

void loop() {    
  int T_count = digitalRead(SW) ;  
  if (T_count == LOW) { count = 0 ; }
  
  currentCLK = digitalRead(CLK);   
  if (currentCLK != previousCLK){ 
    if (digitalRead(DT) != currentCLK) { 
      count --;
     }
     else {      
      count ++;
    }
    Serial.print(" count = ");
    Serial.println(count);
  } 
  previousCLK = currentCLK; 
}