[問題] 關於編碼器程式碼一問ARDUINO
各位板上大大 大家好 小弟之前友發問過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
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章