[問題] 關於編碼器程式碼一問ARDUINO

看板C_and_CPP (C/C++)作者 (sbk8027)時間12年前 (2013/08/13 23:59), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
各位板上大大 大家好 小弟之前友發問過ARDUINO來撰寫編碼器 再次付上編碼器網址 http://www.shayye.com.tw/pdf/Magnetic-Encoders.pdf 這次改用別種寫法如下 #define ENCODER_A_PIN 2 #define ENCODER_B_PIN 3 long position; void setup() { Serial.begin(19200); Serial.println("Started"); pinMode(ENCODER_A_PIN, INPUT); pinMode(ENCODER_B_PIN, INPUT); attachInterrupt(0, read_quadrature, CHANGE); } void loop() { Serial.print("Position: "); Serial.println(position, DEC); delay(1000); } void read_quadrature() { // found a low-to-high on channel A if (digitalRead(ENCODER_A_PIN) == HIGH) { // check channel B to see which way if (digitalRead(ENCODER_B_PIN) == LOW) position++; else position--; } // found a high-to-low on channel A else { // check channel B to see which way if (digitalRead(ENCODER_B_PIN) == LOW) position--; else position++; } } 是有跑出來數值如下 Started Position: 0 Position: 0 Position: 0 Position: 0 Position: 0 Position: 0 Position: 0 Position: -82通電後 Position: -168 Position: -219 Position: -302 Position: -388 Position: -428 Position: -548 Position: -621 Position: -707 Position: -796 Position: -890 Position: -978 Position: -1058 Position: -1144 Position: -1210斷電後 Position: -1210 Position: -1210 Position: -1210 Position: -1210 但數值相當奇怪是負值且沒有一個規律性 想請問是哪裡出了問題 訊號輸出電路我是照PDF檔中電路去接的 採用TWO CHNNEL 然後ARDUINO UNO的PIN2拉到A&B 麻煩各位大大了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.233.40.22
文章代碼(AID): #1I2bV-1z (C_and_CPP)
文章代碼(AID): #1I2bV-1z (C_and_CPP)