Optimal and Save Water Pump Control I'm using an Arduino Micro, a 12 V water pump and a print relay to water a plant.

A water level switch monitors the the water tank to make sure the water pump does not run dry.

I tried different watering times and figured that when using longer watering times 90,000 milliseconds and longer. The relay sometimes does not switch correctly and the pumps keeps running. I'm not sure if my code is optimal for that kind of purpose:

const int Power_Water = 3; // Pin to turn the Pump On and Off via relay
const int Watering_Time = 90000; // Time the water's supposed to run
const int Water_Tank = 10; // Water Level Switch


void setup() {
  Serial.begin(9600);
  pinMode(Power_Water,OUTPUT); // Pin that triggers the Relay as Output
  pinMode (Water_Tank,INPUT_PULLUP); // Pin of Water_Level Sensor as internal pullup
}

void loop (){
  unsigned long watering_Start = millis();

  while (millis() - watering_Start &lt;= Watering_Time &amp;&amp; (digitalRead (Water_Tank) == LOW)){
    digitalWrite(Power_Water,HIGH); // Water Pump goes ON
    Serial.println(millis() - watering_Start);
  }

  if (millis() - watering_Start &gt; Watering_Time || (digitalRead (Water_Tank) == HIGH)){
    Serial.println("Pump OFF"); 
    digitalWrite(Power_Water,LOW);// Water Pump OFF
  }
  delay(100000000); // example "sleep" time the system is not supposed to water
}


Any suggestions are appreciated.

I changed 


const int Power_Water = 3; // Pin to turn the Pump On and Off via relay
const long Watering_Time = 90000; // Time the water's supposed to run
const long Water_Tank = 10; // Water Level Switch


void setup() {
  Serial.begin(9600);
  pinMode(Power_Water,OUTPUT); // Pin that triggers the Relay as Output
  pinMode (Water_Tank,INPUT_PULLUP); // Pin of Water_Level Sensor as internal pullup
}

void loop (){
  unsigned long watering_Start = millis();
  digitalWrite(Power_Water,HIGH);
while (digitalRead(Power_Water) == HIGH){
Serial.println(millis() - watering_Start);
if (millis() - watering_Start &gt; Watering_Time || (digitalRead (Water_Tank) == HIGH)){
    Serial.println("Pump OFF");
    digitalWrite(Power_Water,LOW);
}
}
delay(100000000); // example "sleep" time the system is not supposed to water
}
That delay statement totally defeats the purposes of your millis code.  If you want to enforce a minimum off time then you'll have to do that with millis as well.  Using millis in this way requires that you allow the loop function to repeat many thousands of times per second.  As written you only allow the loop function to run through once every eleven and a half days.