Hi All
I am trying to get a soil moisture sensor to turn on and off, to prevent corrosion of my probe. (2 secs on, 1 hour off)
I am using a relay for my water pump to turn on and off based on the sensor reading, AND an unsigned long timing. (5 secs on, 1 hour off)
I do not want the pump to pump for more than a few seconds every hour, even if the moisture is low. I have had issues with sensor corrosion/electrolysis, and the entire system flooded.
So I and trying to use 2 x unsigned long timing setups. Maybe I need to shuffle some code around, or try and alternative.
Let me know if you can help me out here. I have a feeling that is could either be some small, minor error, or the thinking behind this sketch isn't any good.
The wiring:
The moisture sensor's voltage Pin is connected to Digital 11 on my Nano v.3 board
and the Moisture sensor signal cable is connected to A5.
The relay is connected to Digital 12.
and the other GND - GND, VCC-5V etc
Code is as follows:
int moisture = A5; //moisture sensor
int vout = 11; //voltage input of moisture sensor
int relayPin = 12; //relay connected to water pump
unsigned long moistureReadOntime = 2000; //amount of time the moisture sensor is on (millis)
unsigned long moistureReadOfftime = 360000; //amount of time the moisture sensor is off (millis)
unsigned long previousMillis1 = 0; // will store last time moisture sensor was updated
unsigned long previousMillis2 = 0; // will store last time relay was updated
unsigned long pumpOnTime = 5000; // amount of time the water pump is on (millis)
unsigned long pumpOffTime = 360000; // amount of time the water pump is off (millis)
int voutState = HIGH; // starts sensor voltage as being on
int relayState = LOW; // starts relay voltage as being off
void setup() {
pinMode(moisture, INPUT); // sets moisture sensor as an input
pinMode(relayPin, OUTPUT); // sets relay as an output
pinMode(vout, OUTPUT); // sets sensor voltage as an output
digitalWrite(vout, voutState); // writes the current state of the sensor voltage
digitalWrite(relayPin, relayState); // writes the current state of the relay
}
// the loop routine runs over and over again forever:
void loop() {
unsigned long currentMillis1 = millis(); // Stores the value of millis() in each iteration
unsigned long currentMillis2 = millis(); // Stores the value of millis() in each iteration
// check to see if the moisture sensor voltage is off, and if its been off for the offtime, and turns it on
if ((voutState = LOW) && (currentMillis1 - previousMillis1) >= moistureReadOfftime)
{ voutState = HIGH;
previousMillis1 = currentMillis1;
digitalWrite(vout, voutState);
// check to see if the moisture sensor voltage is on, and if its been on for the ontime, and turns it off
}
else if ((voutState = HIGH) && (currentMillis1 - previousMillis1) >= moistureReadOntime)
{ voutState = LOW;
previousMillis1 = currentMillis1;
digitalWrite(vout, voutState);
}
//reads the moisture sensor
int sensorValue = analogRead(moisture);
int value = analogRead(moisture);
delay(100);
// if the moisture value is above 500, turns relay off.
if (value > 500)
{ relayState = LOW;
digitalWrite(relayPin, relayState);
}
//checks if the moisture value is below 500, and if the offtime has passed, and turns the relay on if true.
else if ((value < 500) && (currentMillis2 - previousMillis2 >= pumpOffTime))
{ relayState = HIGH;
previousMillis2 = currentMillis2;
digitalWrite(relayPin, relayState);
}
//checks if the relay is on, and if the ontime has passed, and turns the relay off if true.
else if ((relayState = HIGH) && (currentMillis2 - previousMillis2 >= pumpOnTime))
{
relayState = LOW;
previousMillis2 = currentMillis2;
digitalWrite(relayPin, relayState);
}
}