From 1e4075a0f5559fadaa4da6320145335a2aa5f8b6 Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Mon, 3 Apr 2023 18:10:46 +0200 Subject: [PATCH] Wind measurement time fixed --- firmware/sensor_wind.ino | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/firmware/sensor_wind.ino b/firmware/sensor_wind.ino index 1656fc3..b354b99 100644 --- a/firmware/sensor_wind.ino +++ b/firmware/sensor_wind.ino @@ -2,6 +2,8 @@ #include "config_user.h" #include "config.h" +#define WIND_DEFAULT_MEAS_INIT 255 + volatile unsigned int anemometerRotations = 0; uint32_t start_meas_wind_time = 0; int interruptNumber; @@ -38,35 +40,37 @@ void start_measure_wind() boolean check_measure_wind_done() { - static uint8_t previous_pin_state = 255; - static uint8_t tmp_pin_state = 255; + static uint8_t previous_pin_state = WIND_DEFAULT_MEAS_INIT; + static uint8_t tmp_pin_state = WIND_DEFAULT_MEAS_INIT; + static uint32_t meas_time_done = 0; +#ifndef SENSOR_WIND_NO_ISR + // interrupt routine based measurement if ((start_meas_wind_time + (WIND_SENSOR_MEAS_TIME_S * 1000)) <= millis()) { -#ifndef SENSOR_WIND_NO_ISR detachInterrupt(interruptNumber); -#endif + return true; + } +#endif + #ifdef SENSOR_WIND_NO_ISR + if (meas_time_done >= (WIND_SENSOR_MEAS_TIME_S * 1000)) + { + meas_time_done = 0; + previous_pin_state = WIND_DEFAULT_MEAS_INIT; + + return true; + } 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) + if (previous_pin_state != tmp_pin_state && previous_pin_state != WIND_DEFAULT_MEAS_INIT) { anemometerRotations++; #ifdef DEBUG @@ -83,8 +87,12 @@ boolean check_measure_wind_done() WDT_FEED(); #endif } while ((start + WIND_SPEED_MEAS_NO_ISR_TIME_MS) >= millis()); -#endif // SENSOR_WIND_NO_ISR + + meas_time_done += WIND_SPEED_MEAS_NO_ISR_TIME_MS; } + +#endif // SENSOR_WIND_NO_ISR + return false; }