Arduino EC11编码器驱动库使用示例介绍
- EC11驱动库:
Versatile_RotaryEncoder
通过EC11编码器驱动库,可以很轻松实现EC11编码器的功能使用。包括EC11编码器的基本功能正反旋转调节,单击、双击、长按功能。
- 功能演示效果:
- 测试对象:ESP8266
EC11编码器原理图
- EC11编码器部分的原理图:
驱动示例代码
#include
#define clk D5
#define dt D6
#define sw D7
void handleRotate(int8_t rotation);
void handlePressRotate(int8_t rotation);
void handleHeldRotate(int8_t rotation);
void handlePress();
void handleDoublePress();
void handlePressRelease();
void handleLongPress();
void handleLongPressRelease();
void handlePressRotateRelease();
void handleHeldRotateRelease();
Versatile_RotaryEncoder *versatile_encoder;
void setup() {
Serial.begin(9600);
versatile_encoder = new Versatile_RotaryEncoder(clk, dt, sw);
versatile_encoder->setHandleRotate(handleRotate);
versatile_encoder->setHandlePressRotate(handlePressRotate);
versatile_encoder->setHandleHeldRotate(handleHeldRotate);
versatile_encoder->setHandlePress(handlePress);
versatile_encoder->setHandleDoublePress(handleDoublePress);
versatile_encoder->setHandlePressRelease(handlePressRelease);
versatile_encoder->setHandleLongPress(handleLongPress);
versatile_encoder->setHandleLongPressRelease(handleLongPressRelease);
versatile_encoder->setHandlePressRotateRelease(handlePressRotateRelease);
versatile_encoder->setHandleHeldRotateRelease(handleHeldRotateRelease);
Serial.println("Ready!");
}
void loop() {
if (versatile_encoder->ReadEncoder()) {
}
}
void handleRotate(int8_t rotation) {
Serial.print("#1 Rotated: ");
if (rotation > 0)
Serial.println("Right");
else
Serial.println("Left");
}
void handlePressRotate(int8_t rotation) {
Serial.print("#2 Pressed and rotated: ");
if (rotation > 0)
Serial.println("Right");
else
Serial.println("Left");
}
void handleHeldRotate(int8_t rotation) {
Serial.print("#3 Held and rotated: ");
if (rotation > 0)
Serial.println("Right");
else
Serial.println("Left");
}
void handlePress() {
Serial.println("#4.1 Pressed");
}
void handleDoublePress() {
Serial.println("#4.2 Double Pressed");
}
void handlePressRelease() {
Serial.println("#5 Press released");
}
void handleLongPress() {
Serial.println("#6 Long pressed");
}
void handleLongPressRelease() {
Serial.println("#7 Long press released");
}
void handlePressRotateRelease() {
Serial.println("#8 Press rotate released");
}
void handleHeldRotateRelease() {
Serial.println("#9 Held rotate released");
}