最近在学习旋转编码器,于是在淘宝上买了这个旋转编码器,还挺贵的,一个7块多,于是去arduino上看看有没有现成的库,
于是我安装了一下这个库,使用了basic基础案例,代码如下:
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(1, 2);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
原来代码是使用的5,6引脚,我这里改成了1,2引脚,下面就是它的一个输出结果,还可以