2014-12-23 105 views
0

我正在用arduino來計算車輪的速度。我使用霍爾效應傳感器。每一秒我都會用它計算出的新RPM來更新我的速度值。如何發送代碼中的一個第二條件之外的數據,而不會影響我的計算中斷之外的Arduino串行通信

// read RPM 

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 
int rpm = 0; 
unsigned long lastmillis = 0; 

void setup(){ 
    Serial.begin(9600); 
    attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2). 
} 

void loop(){ 

    if (millis() - lastmillis == 1000){ /*Uptade every one second, this will be equal to reading   frecuency (Hz).*/ 

    detachInterrupt(0); //Disable interrupt when calculating 


    rpm = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per  full rotation. For two interrups per full rotation use rpmcount * 30.*/ 

    Serial.print("RPM =\t"); //print the word "RPM" and tab. 
    Serial.print(rpm); // print the rpm value. 
    Serial.print("\t Hz=\t"); //print the word "Hz". 
    Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/ 

    rpmcount = 0; // Restart the RPM counter 
    lastmillis = millis(); // Uptade lasmillis 
    attachInterrupt(0, rpm_fan, FALLING); //enable interrupt 
    } 
} 


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/ 
    rpmcount++; 
} 

我需要更新一些其它的值每50毫秒,如何做到這一點? 謝謝

回答

1

您可以使用TimeOne.h添加一個ISR在50ms發生,類似於attachInterrupt()。還有Timer2庫。定時器功能通常用於產生PWM或硬件引腳功能。這些庫將它們的中斷配置爲溢出並將它們從其相關引腳斷開。

注意Arduino Core庫使用Timer0生成1ms中斷來更新millis()計數器。除非在其他第二方庫中使用,否則Timer1和2通常可以免費使用。

+0

好,我會試試這個解決方案。謝謝 –