#include "config_user.h" #include "config.h" volatile unsigned int anemometerRotations = 0; uint32_t start_meas_wind_time = 0; int interruptNumber; #ifndef SENSOR_WIND_NO_ISR void ICACHE_RAM_ATTR _anemometerInterrupt() { anemometerRotations++; #ifdef DEBUG Serial.print("*"); debug("*"); #endif } #endif float wind_speed() { start_measure_wind(); do { delay(1000); // minimum delay of measurement time is 1 second } while (check_measure_wind_done() == false); return measure_wind_result(); } void start_measure_wind() { start_meas_wind_time = millis(); anemometerRotations = 0; #ifndef SENSOR_WIND_NO_ISR interruptNumber = digitalPinToInterrupt(ANEMOMETER_PIN); attachInterrupt(interruptNumber, _anemometerInterrupt, FALLING); #endif } boolean check_measure_wind_done() { static uint8_t previous_pin_state = 255; static uint8_t tmp_pin_state = 255; if ((start_meas_wind_time + (WIND_SENSOR_MEAS_TIME_S * 1000)) <= millis()) { #ifndef SENSOR_WIND_NO_ISR detachInterrupt(interruptNumber); #endif return true; #ifdef SENSOR_WIND_NO_ISR } else { // measure wind without ISR uint32_t start = millis(); /*#ifdef DEBUG Serial.print("."); debug("."); #endif*/ do { // read pin state previous_pin_state = tmp_pin_state; tmp_pin_state = digitalRead(ANEMOMETER_PIN); /*#ifdef DEBUG Serial.print("+"); debug("+"); #endif*/ if (previous_pin_state != tmp_pin_state && previous_pin_state != 255) { anemometerRotations++; #ifdef DEBUG Serial.print("*"); debug("*"); #endif } // wait 5 ms delay(5); #ifdef ENABLE_WATCHDOG // feed the watchdog to prevent reset WDT_FEED(); #endif } while ((start + WIND_SPEED_MEAS_NO_ISR_TIME_MS) >= millis()); #endif // SENSOR_WIND_NO_ISR } return false; } float measure_wind_result() { start_meas_wind_time = 0; #ifdef DEBUG debug("rotations = " + String((float)anemometerRotations)); #endif return (float)anemometerRotations * WINDSPEED_FACTOR; }