一:导入编码器库
二:代码
#include <Arduino.h>
#include <ESP32Encoder.h>
//按键配置
#define EC11_A_PIN 9
#define EC11_B_PIN 8
#define EC11_K_PIN 12
ESP32Encoder encoder;
void ec11_init()
{
encoder.attachSingleEdge(EC11_A_PIN, EC11_B_PIN);
pinMode(EC11_K_PIN, INPUT_PULLUP);
}
int lastEncoderValue = 0;
int ec11_scan(void)
{
if (lastEncoderValue != encoder.getCount())
{
int now_count = encoder.getCount();
if (now_count > lastEncoderValue)
{
Serial.println("left");
}
else
{
Serial.println("right");
}
lastEncoderValue = now_count;
Serial.print("Encoder value: ");
Serial.println(lastEncoderValue);
}
if (digitalRead(EC11_K_PIN) == LOW)
{
delay(20);
if (digitalRead(EC11_K_PIN) == LOW)
{
Serial.println("K");
}
while (digitalRead(EC11_K_PIN) == LOW)
;
}
}
无脑调用库
做学习笔记用