[問題] arduino寫encoder
小弟最近在做自走車
http://www.shayye.com.tw/pdf/Magnetic-Encoders.pdf
以上是所使用的編碼器
搭配ARDUINO UNO
想寫編碼器去讀編碼器的數值
int val;
int encoder0PinA = 2;
int encoder0PinALast = LOW;
int n = LOW;
void setup() {
pinMode (encoder0PinA,INPUT);
Serial.begin (9600);
}
void loop() {
n = digitalRead(encoder0PinA);
Serial.print (n);
delay(500);
}
以上是小弟的寫法,但這樣只能顯示0跟1
想請問各位TIMER要怎麼寫
然後小弟爬文後修改別人的code
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void setup()
{
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
digitalWrite(2, HIGH);
Serial.begin(9600);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
if (rpmcount >= 0) {
rpm = 3000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.print("RPM: ");
Serial.print(rpm);
Serial.print("\n");
delay(1000);
}
}
void rpm_fun()
{
rpmcount++;
}
發現有讀道數值但 跑得很快根本不知道在跑什麼只知道他數直一直跑一直跑
是需要修改哪裡嗎
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.233.40.22
→
08/11 22:01, , 1F
08/11 22:01, 1F
→
08/11 22:03, , 2F
08/11 22:03, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章