11/*
2- *---------------------------------------------------------------------------------
2+ *---------------------------------------------------------------------------------
33 *
44 * Copyright (c) 2025, SparkFun Electronics Inc.
55 *
@@ -29,21 +29,17 @@ SparkFunSoilMoistureSensor mySoilSensor; // Create an instance of the sensor cla
2929// 100% Wet = off
3030//
3131// To do this, the following is done:
32- // 1. Read the sensor value
32+ // 1. Get the sensor value as a ration of moisture (0 - 1.0)
3333// 2. Calculate the percentage of wetness and multiply by 10 using integer math (floor)- this gives a value of 0 to 10
34- // 3. The LED flash_rate = value * increment_value, if value is 0, rate = <fast_value> seconds a value of 1 second for 0% wetness and
34+ // 3. The LED flash_rate = value * increment_value, if value is 0, rate = <fast_value> seconds a value of 1 second for
35+ // 0% wetness and
3536// "off" for > 90% wetness
3637
37-
38-
39- // Define our max dry value - note using a float here
40- #define MAX_DRY_VALUE 1024.0
41-
4238// Min flash rate in ms (2 seconds)
4339#define FAST_FLASH_RATE 2000
4440
4541// Max flash rate in ms (1 day) - basically off
46- #define MAX_FLASH_RATE 86400000
42+ #define LONG_FLASH_RATE 86400000
4743
4844// Define our increment value = 10000ms (10 seconds)
4945#define FLASH_INCREMENT 10000
@@ -93,8 +89,9 @@ void setup()
9389 // Check if the sensor is connected and initialize it
9490 if (mySoilSensor.begin () == false )
9591 {
96- Serial.println (" Soil Moisture Sensor not detected at default I2C address. Verify the sensor is connected. Stopping." );
97-
92+ Serial.println (
93+ " Soil Moisture Sensor not detected at default I2C address. Verify the sensor is connected. Stopping." );
94+
9895 while (1 )
9996 ;
10097 }
@@ -105,83 +102,60 @@ void setup()
105102 Serial.println ();
106103 Serial.println (" LED will flash based on the soil moisture reading:" );
107104 Serial.print (" < 10% Wet = 1 flash every " );
108- Serial.print (FAST_FLASH_RATE/ 1000 );
105+ Serial.print (FAST_FLASH_RATE / 1000 );
109106 Serial.println (" seconds" );
110107 Serial.print (" 10%-90% Wet = flash rate proportional to the percentage of wetness: " );
111- Serial.print (FLASH_INCREMENT/ 1000 );
108+ Serial.print (FLASH_INCREMENT / 1000 );
112109 Serial.println (" seconds per 10% range" );
113110 Serial.println (" > 90% Wet = LED off" );
114111 Serial.println ();
115112
116113 mySoilSensor.LEDOff ();
117114
118- // setup the initial blink rate bin - off
119- blinkRate= MAX_FLASH_RATE ;
115+ // setup the initial blink rate bin - off
116+ blinkRate = LONG_FLASH_RATE ;
120117
121118 // initial last blink time - now!
122- lastBlinkTime = millis ();
119+ lastBlinkTime = millis ();
123120
124121 Serial.println (" Reading soil moisture sensor..." );
125122 Serial.println ();
126-
127-
128- }
129-
130- // ----------------------------------------------------------------------------------------
131- void loop ()
132- {
133- // Let's get the soil moisture reading
134- uint16_t soilMoisture = mySoilSensor.readMoistureValue ();
135-
136- // Wet ration 0 - 1.0
137- float wetRatio = ((MAX_DRY_VALUE - (float )soilMoisture) / MAX_DRY_VALUE);
138-
139- // Output the value:
140- Serial.print (" Soil Moisture: " );
141- Serial.print (soilMoisture);
142- Serial.print (" (sensor value), " );
143- Serial.print (wetRatio * 100 );
144- Serial.println (" % wet" );
145-
146- // Update the blink rate based on the soil moisture reading
147- updateBlinkRate (wetRatio);
148-
149- // check if we need to flash the LED
150- checkForLEDBlink ();
151-
152- // delay our reading.
153- delay (LOOP_DELAY);
154123}
155124
156125// ----------------------------------------------------------------------------------------
157- // A function to update the blink rate based on the soil moisture ration (0 - 1.0)
158- void updateBlinkRate (float wetRatio)
126+ // A function to get the blink rate based on the soil moisture ration (0 - 1.0)
127+ uint32_t getBlinkRate (float wetRatio)
159128{
129+ // our return value
130+ uint32_t newBlinkRate = 0 ;
160131
161132 // Calculate the delay rate "bin" (wet ratio * 10) for the LED based on this current reading
162133 // The rate bin is a value from 0 to 10
163- uint32_t blinkRateBin = wetRatio * 10 ;
134+ uint32_t blinkRateBin = wetRatio * 10 ;
164135
165- // What is the blink rate in ms?
166- if (blinkRateBin == 0 )
136+ // Determine the blink rate based on the bin
137+ switch (blinkRateBin)
167138 {
139+ case 0 :
168140 // 0% wet - Flash at the fast rate
169- blinkRate = FAST_FLASH_RATE;
170- }
171- else if (blinkRateBin > 8 )
172- {
173- // 90% - 100% wet - LED off
174- blinkRate = MAX_FLASH_RATE; // 1 day - basically off
175- }else {
141+ newBlinkRate = FAST_FLASH_RATE;
142+ break ;
143+ case 9 :
144+ case 10 :
145+ // rate > 80% (90% - 100%)
146+ newBlinkRate = LONG_FLASH_RATE; // 1 day - basically off
147+ break ;
148+ default :
176149 // 10%-90% wet - rate proportional to the percentage of wetness
177- blinkRate = blinkRateBin * FLASH_INCREMENT;
150+ newBlinkRate = blinkRateBin * FLASH_INCREMENT;
178151 }
179152
153+ return newBlinkRate;
180154}
181155
182156// ----------------------------------------------------------------------------------------
183157// A function to check if we need to flash the LED based on elapsed time from last blink
184- void checkForLEDBlink (void )
158+ void checkForLEDBlink (void )
185159{
186160 // do we need to flash the LED?
187161 if (millis () - lastBlinkTime > blinkRate)
@@ -193,4 +167,33 @@ void checkForLEDBlink(void )
193167
194168 lastBlinkTime = millis ();
195169 }
196- }
170+ }
171+ // ----------------------------------------------------------------------------------------
172+ void loop ()
173+ {
174+ // Output the value:
175+ Serial.print (" Soil Moisture: " );
176+ Serial.print (mySoilSensor.readMoistureValue ());
177+ Serial.print (" (sensor value), " );
178+
179+ // Now the percent moisture
180+ Serial.print (mySoilSensor.readMoisturePercentage ());
181+ Serial.println (" % wet" );
182+
183+ // The current blink rate based on the soil moisture ratio reading
184+ uint32_t newBlinkRate = getBlinkRate (mySoilSensor.readMoistureRatio ());
185+
186+ // update the blink rate if it has changed
187+ if (newBlinkRate != blinkRate)
188+ {
189+ blinkRate = newBlinkRate;
190+ Serial.print (" New blink delay: " );
191+ Serial.print (blinkRate);
192+ Serial.println (" ms" );
193+ }
194+ // check if we need to flash the LED
195+ checkForLEDBlink ();
196+
197+ // delay our loop.
198+ delay (LOOP_DELAY);
199+ }
0 commit comments