#define ENCODER_CLK 22
#define ENCODER_DT 21
#include <ESP32Servo.h>
int APin = 33;
ESP32PWM pwm;
int freq = 1000;
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(33, INPUT);
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
Serial.begin(115200);
pwm.attachPin(APin, freq, 10); // 1KHz 8 bit
}
int click = 0;
int lastClk = HIGH;
void loop() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
click += 10;
Serial.println(click);
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
click -= 10;
Serial.println(click);
}
}
pwm.writeScaled(click);
freq = 1000;
pwm.adjustFrequency(freq, 0.0); // reset the time base
}