Hey everyone, I am a little at a loss here, likely some silly thing, but here's my boggle. I built a box running a clone nano (w/328) that is supposed to read a pot every 500 ms, read analog and digital sensors every hour, then based on the input from the pot and sensors, water a plant, or dont, every 8 hours. Basically if a moisture sensor reads higher (dryer) than threshold (pot) I want it to water the plant, as long as the other sensors for water supply and overflow are in "normal" state.
I get good reads from the sensors and the pot at the intervals I want, but as soon as i adjust the pot to a value lower than the moisture sensors value, it immediately triggers a watering. It doesn't wait for the 8 hour mark. It also repeats the watering cycle over and over (as it hasn't read the moisture sensor to tell it the soil is wet again yet). What it should do is wait for the 8 hour mark, water if needed, then wait again till the next 8 hour mark and water again if need be...
So to sum up the issue, everything seems to work as expected, except the watering itself. It doesn't wait to do its thing, and it does it over and over. I have included the whole void loop code, as I'm not sure where the issue is. Thanks everyone!
void loop()
{
//Threshold detection cycle every .5 seconds begin:
unsigned long currentMillis = millis();
if(currentMillis - previousMillis1 > thresholdDelay)
{
previousMillis1 = currentMillis;
threshold_val = analogRead(threshold); //read the value from the potentiometer
lcd.home (); // go home
lcd.clear();
lcd.print("Soil: ");
lcd.print( moisture_val );
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("Threshold: ");
lcd.print( threshold_val );
}
// Power up and Read the sensors once every hour
if(currentMillis - previousMillis2 > sensorDelay)
{
previousMillis2 = currentMillis;
digitalWrite(13, HIGH); //LED to show that Moisture detection cycle is in progress
digitalWrite(moisturePWR, HIGH); //Turn on power for the moisture probe
digitalWrite(leakINtrayPWR, HIGH); //Turn on power for the Overflow sensor
delay(100); //small delay to let the sensor settle
moisture_val = analogRead(moistureSensor); // read the value from the moisture-sensing probe, Dryer the soil, higher the value
reservoir_val = digitalRead(reservoirProbe);
leakINtray_val = digitalRead(leakINtray); //check to see if the tray is going to overflow
Serial.print("Reservoir State "); //for debugging
Serial.println(reservoir_val);
Serial.print("Moisture "); //for debugging
Serial.println(moisture_val);
Serial.print("Threshold "); //for debugging
Serial.println(threshold_val);
Serial.print("Overflow State "); //for debugging
Serial.println(leakINtray_val);
digitalWrite(moisturePWR, LOW); //Power to probe off
digitalWrite(leakINtrayPWR, LOW); //Turn off power for the Overflow sensor
digitalWrite(13, LOW); //turn off LED that showed moisture detection cycle in progress (cycle complete)
}
//send signals for emails and water the plant on 8 hour cycle
if(currentMillis - previousMillis3 > cycleDelay)
{
if (reservoir_val == HIGH) //if water level is good:
{
reservoirProbeTX(); //Send signal to ethernet link to trigger email about needing water
}
else
{
}
if (leakINtray_val == LOW)
{
overFlow(); //Send signal to ethernet link to trigger email about overflow imminent
}
else {}
//Watering (if the soil is too dry, AND there is water available in the tank, AND the tray isnt overflowing AND no leak in housing, turn on pump (pin 4), otherwise, do nothing.
if (moisture_val >=threshold_val && reservoir_val == LOW && leakINtray_val == HIGH) // && leakINhousing_val == HIGH)
{
watering(); //turn on pump, send signal to trigger email advising of plant being watered
}
else {}
}
}