From d64710a841483cea2224d3baf5671a22e4f0a23d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Uribe=20Stengel?= Date: Wed, 1 Jul 2020 16:54:11 +0200 Subject: [PATCH] Prevent moisture to exceed range from 0-100 percent. --- src/capacitiveSoilMoistureSensor.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/capacitiveSoilMoistureSensor.cpp b/src/capacitiveSoilMoistureSensor.cpp index ff6ff1d..3b67af6 100644 --- a/src/capacitiveSoilMoistureSensor.cpp +++ b/src/capacitiveSoilMoistureSensor.cpp @@ -24,5 +24,10 @@ int readCapacitiveSoilMoistureSensor() Serial.println(measurement); // add the reading to the total: //int measurement = analogRead(PIN_MS); - return map(measurement, VALUE_AIR, VALUE_WATER, 0, 100); + + if (map(measurement, VALUE_AIR, VALUE_WATER, 0, 100) < 0) { + return 0; + } else if (map(measurement, VALUE_AIR, VALUE_WATER, 0, 100) > 100) { + return 100; + } else {return map(measurement, VALUE_AIR, VALUE_WATER, 0, 100);} } \ No newline at end of file