後來又試著另一個程式碼,也是風速計。
How to Hookup Davis Anemometer to Arduino (Part 2 of 3)
關於ISR function,我有查看它的說明是:不能回傳值,以及不可以使用時間函數,如:time, millis, delay
但是就是不知道應該怎麼改,要怎麼把debounced 判斷拉出來放在新的函數 (就是因為要判斷 debounced 所以才會出現時間函數)。
風速計程式碼2:
#include <math.h>
#define WindSensorPin (2) // The pin location of the anemometer sensor
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine
float WindSpeed; // speed miles per hour
void setup() {
Serial.begin(9600);
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), isr_rotation, FALLING);
Serial.println("Davis Wind Speed Test");
Serial.println("Rotations\tMPH");
}
void loop() {
Rotations = 0; // Set Rotations count to 0 ready for calculations
sei(); // Enables interrupts
delay (3000); // Wait 3 seconds to average
cli(); // Disable interrupts
/* convert to mp/h using the formula V=P(2.25/T)
V = P(2.25/3) = P * 0.75 */
WindSpeed = Rotations * 0.75;
Serial.print(Rotations); Serial.print("\t\t");Serial.println(WindSpeed);
}
// This is the function that the interrupt calls to increment the rotation count
void isr_rotation () {
if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}
}
結果呢...還是顯示0