user51476: Hi so I am making a self watering flower pot with a moisture sensor and a peristaltic pump, the code is as follows:

I want to add an indicator LED which turns on if the sensor value has been equal to or greater than MaxDryness for longer than twenty minutes.
How would I alter the code to do that?

user37735: Following is an example how it could be coded.
Remember that the soil is dry by setting DrySoil = true.
Register the time at which a dry period starts in DrySoilBegin.
Check at each 'dry' reading whether DrySoil was true for more than 20 minutes.
Set DrySoil = false at every 'not dry' reading.
The ul after the constant 1200000 makes it an unsigned long value. This because millis() returns a unsigned long result. I am not 100% sure this is necessary (maybe the compiler takes care of this automatically).
As I don't have a plant watering system I couldn't verify the proper working..
#define WATERPIN 10
#define READSOILPIN A0
#define LEDPIN 13

// higher number is more dry
#define MAXDRYNESS 700

#define WATERDELAY 10000
#define WATERPOSTDELAY 5000

bool DrySoil = false; // State of soil
unsigned long DrySoilBegin; // Time when DrySoil became true

void setup() {
  Serial.begin(9600);
  pinMode(READSOILPIN, INPUT);
  pinMode(WATERPIN, OUTPUT);
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  int SensorValue = analogRead(READSOILPIN); //take a sample
  Serial.print(SensorValue);
  Serial.print(" ־ ");

  if(SensorValue >= MAXDRYNESS) {
    // Soil is too dry
    // Check if soil was already in a dry state
    if(DrySoil){
      // If already in a dry state check the duration
      if((millis()-DrySoilBegin) > 1200000ul){
        // If duration is longer than 20 minutes switch LED on
        //   or keep LED switched on
        digitalWrite(LEDPIN, HIGH);
      }
    }
    else{
      // First 'dry' result after 'wet' result(s)
      DrySoil = true; // Set DrySoil in true state
      DrySoilBegin = millis(); // Set beginning of DrySoil period
    }
    // start watering for 10 seconds then
    //    wait for 5 seconds before monitoring again
    Serial.println("Soil dry start watering");
    digitalWrite(WATERPIN, HIGH);
    delay(WATERDELAY); // wait until WATERDELAY milliseconds have passed
    digitalWrite(WATERPIN, LOW);
    delay(WATERPOSTDELAY); // wait until WATERPOSTDELAY milliseconds have passed
  }
  else{
    // Soil is not too dry
    DrySoil = false; // Set or keep DrySoil in state false
    digitalWrite(LEDPIN, LOW); // Switch LED off, or keep it switched off
    delay(50); // Wait 50 ms before next sampling
  }
}


51476: <p>Hi so I am making a self watering flower pot with a moisture sensor and a peristaltic pump, the code is as follows:</p>

<p><a href="https://i.stack.imgur.com/5EDeQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5EDeQ.png" alt="enter image description here"></a></p>

<p>I want to add an indicator LED which turns on if the sensor value has been equal to or greater than MaxDryness for longer than twenty minutes.</p>

<p>How would I alter the code to do that?</p>
 37735: <p>Following is an example how it could be coded.</p>

<p>Remember that the soil is dry by setting <code>DrySoil = true</code>.</p>

<p>Register the time at which a dry period starts in DrySoilBegin.</p>

<p>Check at each 'dry' reading whether DrySoil was true for more than 20 minutes.</p>

<p>Set <code>DrySoil = false</code> at every 'not dry' reading.</p>

<p>The <code>ul</code> after the constant <code>1200000</code> makes it an unsigned long value. This because <code>millis()</code> returns a <code>unsigned long</code> result. I am not 100% sure this is necessary (maybe the compiler takes care of this automatically).</p>

<p>As I don't have a plant watering system I couldn't verify the proper working..</p>

<pre><code>#define WATERPIN 10
#define READSOILPIN A0
#define LEDPIN 13

// higher number is more dry
#define MAXDRYNESS 700

#define WATERDELAY 10000
#define WATERPOSTDELAY 5000

bool DrySoil = false; // State of soil
unsigned long DrySoilBegin; // Time when DrySoil became true

void setup() {
  Serial.begin(9600);
  pinMode(READSOILPIN, INPUT);
  pinMode(WATERPIN, OUTPUT);
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  int SensorValue = analogRead(READSOILPIN); //take a sample
  Serial.print(SensorValue);
  Serial.print(" ־ ");

  if(SensorValue &gt;= MAXDRYNESS) {
    // Soil is too dry
    // Check if soil was already in a dry state
    if(DrySoil){
      // If already in a dry state check the duration
      if((millis()-DrySoilBegin) &gt; 1200000ul){
        // If duration is longer than 20 minutes switch LED on
        //   or keep LED switched on
        digitalWrite(LEDPIN, HIGH);
      }
    }
    else{
      // First 'dry' result after 'wet' result(s)
      DrySoil = true; // Set DrySoil in true state
      DrySoilBegin = millis(); // Set beginning of DrySoil period
    }
    // start watering for 10 seconds then
    //    wait for 5 seconds before monitoring again
    Serial.println("Soil dry start watering");
    digitalWrite(WATERPIN, HIGH);
    delay(WATERDELAY); // wait until WATERDELAY milliseconds have passed
    digitalWrite(WATERPIN, LOW);
    delay(WATERPOSTDELAY); // wait until WATERPOSTDELAY milliseconds have passed
  }
  else{
    // Soil is not too dry
    DrySoil = false; // Set or keep DrySoil in state false
    digitalWrite(LEDPIN, LOW); // Switch LED off, or keep it switched off
    delay(50); // Wait 50 ms before next sampling
  }
}
</code></pre>
