#include <ESP32Servo.h>
#define ENCODER_CLK 34
#define ENCODER_DT 35
#define ENCODER_SW 32
volatile int cuenta =90;
volatile int lastClk = HIGH;
volatile int incremento=5;
Servo servo_1;
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
attachInterrupt(digitalPinToInterrupt(ENCODER_SW), goHome, FALLING );
servo_1.attach(23);
servo_1.write(cuenta);
refrescar ();
}
void loop() {
delay(100);
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == LOW) {
cuenta=cuenta-incremento;
if (cuenta<0){
cuenta=0;
}
servo_1.write(cuenta);
}
if (dtValue == HIGH) {
cuenta=cuenta+incremento;
if (cuenta>180){
cuenta=180;
}
servo_1.write(cuenta);
}
refrescar ();
}
void goHome(){
cuenta=90;
servo_1.write(cuenta);
Serial.println("Yendo a posicion Incial....");
refrescar ();
}
void refrescar() {
Serial.print("Actual :");
Serial.println(cuenta);
}