From dbf212bdea8efd9f44a5c2be741b14e8994833b3 Mon Sep 17 00:00:00 2001 From: GioCC <25667790+GioCC@users.noreply.github.com> Date: Thu, 13 Oct 2022 20:21:01 +0200 Subject: [PATCH] Fix faulty initialization of analog value at startup (additional fix for #199) (#212) --- src/MF_Analog/MFAnalog.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MF_Analog/MFAnalog.cpp b/src/MF_Analog/MFAnalog.cpp index 69566581..473c6700 100644 --- a/src/MF_Analog/MFAnalog.cpp +++ b/src/MF_Analog/MFAnalog.cpp @@ -13,11 +13,13 @@ MFAnalog::MFAnalog(uint8_t pin, const char *name, uint8_t sensitivity) _sensitivity = sensitivity; _pin = pin; _name = name; - _lastValue = 0; pinMode(_pin, INPUT_PULLUP); // set pin to input. Could use OUTPUT for analog, but shows the intention :-) + // Fill averaging buffers with initial reading for (uint8_t i = 0; i < ADC_MAX_AVERAGE; i++) { readBuffer(); } + // and set initial value from buffers + _lastValue = ADC_Average_Total >> ADC_MAX_AVERAGE_LOG2; } bool MFAnalog::valueHasChanged(int16_t newValue)