#define CLK 2
#define DT 3
unsigned long lastReset;
float N=20;
int counter = 0;
float speed_counter=0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
float TP=300;
void setup() {
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
// Call updateEncoder() when any high/low changed seen
// on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
void loop()
{
if (millis() - lastReset > TP)
{
lastReset += TP;
speed_counter=counter;
float speed;
speed = 2*3.14*1000*speed_counter/(N*TP);
Serial.print("Counter is ");
Serial.print(speed_counter);
Serial.print("\n");
Serial.print("Speed is ");
Serial.print(speed);
counter = 0;
Serial.print("\n\n");
}
}
void updateEncoder(){
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter ++;
currentDir ="CW";
} else {
// Encoder is rotating CW so increment
counter --;
currentDir ="CCW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
}