滑鼠拆下來的滾輪編碼器 時序為 → CW A ----------- ------------- HIGH ---------- LOW ← CCW B -------- ---------------- HIGH ---------- LOW A B 1 1↓CW 1 0 0 0 0 1 1 1 1 0 0 0 0 1↑CCW 程式碼參考底下網址來源修改而來 #define ENCODER_A_PIN 2 #define ENCODER_B_PIN 3 unsigned long time = 0; long position = 0; long num = 0; void setup() { pinMode(ENCODER_A_PIN,INPUT); pinMode(ENCODER_B_PIN,INPUT); attachInterrupt(0, RotaryEncoder, LOW); Serial.begin(9600); time = millis(); } void loop() { while (num != position) { num = position; Serial.println(num); } } void RotaryEncoder(){ int temp =digitalRead(ENCODER_B_PIN); if ((millis() - time) > 3) { if ( temp == LOW) { position++; }else{ if ( temp == HIGH) position--; } } time = millis(); } 沒中斷版本 int trigger=0; void Rotar
留言
張貼留言