@@ -43,15 +43,21 @@ SparkFunSoilMoistureSensorI2C mySoilSensor; // Create an instance of the sensor
4343// Define our increment value = 10000ms (10 seconds)
4444#define FLASH_INCREMENT 10000
4545
46- // Time between loop iterations - 5 second (should not be greater that FAST_FLASH_RATE)
46+ // Time between loop iterations - Just make it the same as our FAST RATE.
4747#define LOOP_DELAY FAST_FLASH_RATE
4848
49+ // Time between Soil Moisture readings - 30 seconds
50+ #define SOIL_MOISTURE_READ_RATE 30000
51+
4952// Define a variable for the last blink time - in ms since boot
5053uint32_t lastBlinkTime = 0 ;
5154
5255// Define a variable for the blink rate - delay between blinks in ms
5356uint32_t blinkRate = 0 ;
5457
58+ // Define the last time we read the soil moisture sensor
59+ uint32_t lastSensorReadTime = 0 ;
60+
5561// Some Dev boards have their QWIIC connector on Wire or Wire1
5662// This #ifdef will help this sketch work across more products
5763
@@ -97,6 +103,11 @@ void setup()
97103
98104 Serial.println (" Soil Moisture Sensor found!" );
99105
106+ // Print out read rate information
107+ Serial.println ();
108+ Serial.print (" Reading soil moisture sensor every " );
109+ Serial.print (SOIL_MOISTURE_READ_RATE / 1000 );
110+ Serial.println (" seconds" );
100111 // print out the LED flash rate information
101112 Serial.println ();
102113 Serial.println (" LED will flash based on the soil moisture reading:" );
@@ -116,6 +127,8 @@ void setup()
116127
117128 // initial last blink time - now!
118129 lastBlinkTime = millis ();
130+ // initial last sensor read time - just 0;
131+ lastSensorReadTime = 0 ;
119132
120133 Serial.println (" Reading soil moisture sensor..." );
121134 Serial.println ();
@@ -168,31 +181,46 @@ void checkForLEDBlink(void)
168181 }
169182}
170183// ----------------------------------------------------------------------------------------
171- void loop ()
184+ // Check the sensor value and update the blink rate if needed
185+ void checkSensorValue (void )
172186{
173- // Output the value:
174- Serial.print (" Soil Moisture: " );
175- Serial.print (mySoilSensor.readMoistureValue ());
176- Serial.print (" (sensor value), " );
177-
178- // Now the percent moisture
179- Serial.print (mySoilSensor.readMoisturePercentage ());
180- Serial.println (" % wet" );
181-
182- // The current blink rate based on the soil moisture ratio reading
183- uint32_t newBlinkRate = getBlinkRate (mySoilSensor.readMoistureRatio ());
184-
185- // update the blink rate if it has changed
186- if (newBlinkRate != blinkRate)
187+ // Need to output/get the sensor value - has the time between reads elapsed?
188+ // Or is this our first check?
189+ if (millis () - lastSensorReadTime > SOIL_MOISTURE_READ_RATE || lastSensorReadTime == 0 )
187190 {
188- blinkRate = newBlinkRate;
189- Serial.print (" New blink delay: " );
190- Serial.print (blinkRate);
191- Serial.println (" ms" );
191+ // Output the value:
192+ Serial.print (" Soil Moisture: " );
193+ Serial.print (mySoilSensor.readMoistureValue ());
194+ Serial.print (" (sensor value), " );
195+
196+ // Now the percent moisture
197+ Serial.print (mySoilSensor.readMoisturePercentage ());
198+ Serial.println (" % wet" );
199+
200+ // The current blink rate based on the soil moisture ratio reading - update?
201+ uint32_t newBlinkRate = getBlinkRate (mySoilSensor.readMoistureRatio ());
202+
203+ // update the blink rate if it has changed
204+ if (newBlinkRate != blinkRate)
205+ {
206+ blinkRate = newBlinkRate;
207+ Serial.print (" New blink delay: " );
208+ Serial.print (blinkRate/1000 );
209+ Serial.println (" seconds" );
210+ }
211+ // update the last read time
212+ lastSensorReadTime = millis ();
192213 }
193- // check if we need to flash the LED
194- checkForLEDBlink ();
214+ }
215+ // ----------------------------------------------------------------------------------------
216+ void loop ()
217+ {
218+ // check if we need to flash the LED
219+ checkForLEDBlink ();
220+
221+ // Need to output/get the sensor value?
222+ checkSensorValue ();
195223
196- // delay our loop.
224+ // loop delay
197225 delay (LOOP_DELAY);
198226}
0 commit comments