#define led_clk 2
#define led_dt 4
#define ENCODER_CLK 14
#define ENCODER_DT 12
#define ENCODER_SW 13
bool data, clk_ea, clk_ef ;
int cont;
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
}
int lastClk = HIGH;
void loop() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// Houve uma mudança no pino CLK
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Girando no sentido horário ⏩");
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Girando no sentido anti-horário ⏪");
}
}
}
/*
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(led_clk, OUTPUT);
pinMode(led_dt, OUTPUT);
clk_ea = digitalRead(ENCODER_CLK);
cont = 0;
Serial.println(cont);
}
void loop() {
data = digitalRead(ENCODER_DT);
clk_ef = digitalRead(ENCODER_CLK);
digitalWrite(led_clk, clk_ef);
digitalWrite(led_dt, data);
if (clk_ef && !clk_ea && data){
cont++;
Serial.println(cont);
}else if (clk_ef && !clk_ea && !data){
cont--;
Serial.println(cont);
}
clk_ea = clk_ef;
// put your main code here, to run repeatedly:
delay(1); // this speeds up the simulation
}
*/