I am currently working an automatic home gardening system. There are two moisture sensors and two solenoid valves to allow water to plant two different zones. Preferably, I want to water the plants for x amount of time. I started programming this and I noticed that with my code below, if one solenoid valve goes off, the entire program will delay until one of the solenoid valves closes and thus the other solenoid valve will not open until the other one is closed.
However, I want to prevent this from happening and I want the program to continue reading the moisture readings while one of the solenoid valves is open so that the other solenoid valve can open whenever the zone needs water as well.
int solenoidPin1 = 1; //connected to Digital 1
int solenoidPin2 = 2; //connected to Digital 2
void setup(){
pinMode(solenoidPin1, OUTPUT);
pinMode(solenoidPin2, OUTPUT);
}
void loop(){
int moisture1 = analogRead(moisturePin1);
int moisture2 = analogRead(moisturePin2);
if (moisture1 <= 250){ //if moisture is less than reading 250
digitalWrite(solenoidPin1, HIGH); //switch solenoid ON
delay(300000); //water for five minutes
digitalWrite(solenoidPin1, LOW); //switch solenoid OFF
}
else {
digitalWrite(solenoidPin1, LOW); //switch solenoid OFF
}
if (moisture2 <= 250){ //if moisture is less than reading 250
digitalWrite(solenoidPin2, HIGH); //switch solenoid ON
delay(300000); //water for five minutes
digitalWrite(solenoidPin2, LOW); //switch solenoid OFF
}
else {
digitalWrite(solenoidPin2, LOW); //switch solenoid OFF
}
}
I was thinking that I should do the following for the conditioning part. Will this work instead for what I'm trying to do?
if (moisture1 <= 250){ //if moisture is less than reading 250
digitalWrite(solenoidPin1, HIGH); //switch solenoid ON
}
else if (moisture1 >= 550){
digitalWrite(solenoidPin1, LOW); //switch solenoid OFF
}
if (moisture2 <= 250){ //if moisture is less than reading 250
digitalWrite(solenoidPin2, HIGH); //switch solenoid ON
}
else if (moisture2 >= 550){
digitalWrite(solenoidPin2, LOW); //switch solenoid OFF
}
Note: I'm using a capacitive moisture sensor, so the output voltage/reading is higher the drier the dirt is.