//Encoder Wokwi code
#define CLK 2 //OUTPUT A
#define DT 3 //OUTPUT B
int counter = 0;
int currentStateCLK;
int lastStateCLK;
string currentDir = "";
void setup()
{
// set encoder pins as inputs
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
// set up serial monitor
Serial.begin(9600);
// read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}
void loop()
{
// 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 = "Counter clockwise";
}
else
{
// encoder is rotating CW so increment
counter ++;
currentDir = "Clockwise";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
//put in a slight delay to help debounce the reading
delay(1);
}