Automating your gardening
We are now going to configure our project so it automatically waters the plant if the humidity falls below a given threshold.
The first step is actually to define two thresholds:
floatlowThreshold = 20.00; floathighThreshold = 25.00;
We need two thresholds here because if we just defined one, the pump will constantly switch between the on and off states.
So we will have the pump turn on when the humidity goes below the low threshold, and turn off when we reach the high threshold again.
Next, we define which pin the relay is connected to:
#define relayPin 15
In the setup()
function of the sketch, we set the relay pin as an output:
pinMode(relayPin, OUTPUT);
In the loop()
function, we constantly check whether the humidity went below the low threshold, or above the high threshold:
if (humidity <lowThreshold) { // Activate pump digitalWrite(relayPin, HIGH); } if (humidity >highThreshold) { // Deactivate pump digitalWrite(relayPin, LOW); }
I only highlighted the most...