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 of 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 <= Watering_Time && (digitalRead (Water_Tank) == LOW)) { digitalWrite(Power_Water,HIGH); // Water Pump goes ON Serial.println(millis() - watering_Start); } if (millis() - watering_Start > 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 > 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 }