0

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 } 
2

1 Answer 1

1

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.

2
  • thanks for the input... .. thats an example and here I want the code to repeat every 100000000 ms but when the pump starts I want to monitor it with millis.. the watering part is the important part for me.. I think I found the right code for my needs and added it to my question Commented May 25, 2018 at 20:19
  • The right code for you will not have any use of the delay function. Using the delay function is breaking your use of millis. Using millis requires that the loop be free to run. You can't use blocking calls like delay at the same time you are using millis for timing and expect anything to work out. Commented May 27, 2018 at 16:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.