1 使用一个中断读取编码器的值, 2 #define PinA 2 //中断0 3 long count = 0; //计数值 4 long num = 0; 5 void setup() 6 { 7 Serial.begin(9600); //窗口初始化 8 pinMode(PinA,INPUT); //D2脚为输入 9 pinMode(3,INPUT); //D3脚为输入 10 attachInterrupt(0, blinkA, RISING); //注册中断0调用函数blinkA 11 } 12 void loop() 13 { 14 while (num != count) 15 { 16 num = count; 17 Serial.println(num); 18 } 19 } 20 void blinkA() 21 { 22 if(digitalRead(3)==1) 23 { count ++;} 24 if(digitalRead(3)==0) 25 {count--;} 26 }