What about checking for rain with the humidity sensor? You can do this at the same time you check the time, but it becomes a very long line. It’s OK to use another if() statement; long-time programmers might tell you this is less efficient, but your garden won’t care if the water comes on a split second later. Much more important is that you can read and understand the program.

WARNING

Be aware of well-intentioned experienced programmers who might show you clever tricks for reducing the number of lines in your program, or for improving efficiency. As you gain experience, it’s good to understand why these tricks work, but as a beginner, you should always choose whatever is easiest for you to understand.

Example 8-3 shows a way to turn the water on only if it’s not raining.

Example 8-3. Turn on the water only if it’s not raining
 if ( ( nowMinutesSinceMidnight >= onOffTimes[valve][ONTIME]) &&
      ( nowMinutesSinceMidnight < onOffTimes[valve][OFFTIME]) ) {
      // Before we turn a valve on make sure it's not raining
      if ( humidityNow > 50 ) { // Arbitrary; adjust as necessary
        // It's raining; turn the valve OFF
        Serial.print(" OFF ");
        digitalWrite(valvePinNumbers[valve], LOW);
      }
      else {
        // No rain and it's time to turn the valve ON
        Serial.print(" ON ");
        digitalWrite(valvePinNumbers[valve], HIGH);
      } // end of checking for rain
 } // end of checking for time to turn valve ON
    else {
      Serial.print(" OFF ");
      digitalWrite(valvePinNumbers[valve], LOW);
    }

Of course we’ll make another function for this. Let’s call it checkTimeControlValves().

We’ll also create a separate function for reading the humidity sensor and the RTC. Let’s call that getTimeTempHumidity().

Now our loop looks something like this:

void loop() {

  // Remind user briefly of possible commands
  Serial.print("Type 'P' to print settings or 'S2N13:45'");
  Serial.println(" to set valve 2 ON time to 13:34");

  // Get (and print) the current date, time, 
  // temperature, and humidity
  getTimeTempHumidity();

  // Check for new time settings:
  expectValveSettings();

  // Check to see whether it's time to turn any
  // valve ON or OFF
  checkTimeControlValves();

  // No need to do this too frequently
  delay(5000); 
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *