/*
VCC = 3.3V
GND = GND
CLK = IO22
DT = IO23
SW = IO21
*/
#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"
#define ROTARY_DT_PIN 23
#define ROTARY_CLK_PIN 22
#define ROTARY_SW_PIN 21
#define ROTARY_VCC_PIN -1
#define ROTARY_STEPS 4 // Instead of changing here, rather change numbers above
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(
ROTARY_DT_PIN, ROTARY_CLK_PIN, ROTARY_SW_PIN, ROTARY_VCC_PIN, ROTARY_STEPS);
void rotary_onButtonClick() {
static unsigned long lastTimePressed = 0; // Soft debouncing
if (millis() - lastTimePressed < 500) {
return;
}
lastTimePressed = millis();
Serial.print("Button pressed ");
Serial.print(millis());
Serial.println(" milliseconds after restart");
}
void rotary_loop() {
// Don't print anything unless value changed
if (rotaryEncoder.encoderChanged()) {
Serial.print("Value: ");
Serial.println(rotaryEncoder.readEncoder());
}
if (rotaryEncoder.isEncoderButtonClicked()) {
rotary_onButtonClick();
}
}
void IRAM_ATTR readEncoderISR() {
rotaryEncoder.readEncoder_ISR();
}
void setup() {
Serial.begin(115200);
// Initialize rotary encoder
rotaryEncoder.begin();
rotaryEncoder.setup(readEncoderISR);
// Set boundaries and if values should cycle or not
// In this example, we will set possible values between 0 and 1000
bool circleValues = false;
rotaryEncoder.setBoundaries(0, 1000, circleValues); // minValue, maxValue, circleValues true|false
// rotaryEncoder.disableAcceleration(); // Acceleration is now enabled by default - disable if not needed
rotaryEncoder.setAcceleration(250); // Larger number = more acceleration; 0 or 1 means disabled acceleration
}
void loop() {
rotary_loop();
delay(50);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4