Clock display stops when watering function runs.

Hi, absolute beginner here.

I'm building a automated irrigation system using these parts:
Arduino Uno
DS1307RTC
Grove Capacitive Soil Moisture Sensor
2x 3v Solenoid Valves
2x 3.7v Li-ion batteries 6800mAh

The process is as follows:
If time=5am
then check soil moisture
if soil moisture =>560
then Solenoid Valves = ON
wait 10mins
Solenoid Valves = OFF
If time =7pm
then check soil moisture
if soil moisture =>560
then Solenoid Valves = ON
wait 10mins
Solenoid Valves = OFF

For some reason, when it hits 5am, the RTC stops printing time in the serial monitor and does not resume after.
No error messages are turning up, so I'm not sure what I'm doing wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  #include <Wire.h>
#include "DS1307.h"

DS1307 clock;//define a object of DS1307 class
const int SOLENOID = 13;
void setup() {
  // put your setup code here, to run once:
pinMode(SOLENOID, OUTPUT);
int sensorValue = analogRead(A0);
Serial.begin(9600);
clock.begin();
clock.fillByYMD(2021, 6, 29); //Jan 19,2013
clock.fillByHMS(04, 59, 50); //15:28 30"
clock.fillDayOfWeek(TUE);//Saturday
clock.setTime();//write time to the RTC chip
}

void loop() {
  // put your main code here, to run repeatedly:
printTime();
Watering ();
}
 
/*Function: Display time on the serial monitor*/
void printTime() {
    clock.getTime();
    Serial.print(clock.hour, DEC);
    Serial.print(":");
    Serial.print(clock.minute, DEC);
    Serial.print(":");
    Serial.print(clock.second, DEC);
    Serial.print("  ");
    Serial.print(clock.dayOfMonth, DEC);
    Serial.print("/");
    Serial.print(clock.month, DEC);
    Serial.print("/");
    Serial.print(clock.year + 2000, DEC);
    Serial.print(" ");
    Serial.print(clock.dayOfMonth);
    Serial.print("*");
    switch (clock.dayOfWeek) { // Friendly printout the weekday
        case MON:
            Serial.print("MON");
            break;
        case TUE:
            Serial.print("TUE");
            break;
        case WED:
            Serial.print("WED");
            break;
        case THU:
            Serial.print("THU");
            break;
        case FRI:
            Serial.print("FRI");
            break;
        case SAT:
            Serial.print("SAT");
            break;
        case SUN:
            Serial.print("SUN");
            break;
    }
    Serial.println(" ");
}
void Watering (){
switch(clock.hour){
    {case 5:
    int sensorValue = analogRead(A0);
    if (sensorValue>=560){
      digitalWrite(SOLENOID, HIGH);
      delay(600000);
      digitalWrite(SOLENOID, LOW);
      delay(3600000);
      }
    }
   {case 19:
     int sensorValue = analogRead(A0);
     if (sensorValue>=560){
      digitalWrite(SOLENOID, HIGH);
      delay(600000);
      digitalWrite(SOLENOID, LOW);
      delay(3600000);
      }
    } 
}
}
delay()'s argument is in milliseconds (1/1000 th of a second).
600000ms = 10 minutes
3600000ms = 1 hour
So Watering won't return to the loop() routine for an hour and 10 minutes (if clock.hour is 5 or 19 and sensorValue >= 560).
Last edited on
@OP
That's right, delay() is a killer, literally. Everything stops until the delay time has expired.
What you probably need to do is to have a clear picture/plan of what you want.
1. And that could be continuous time display with regular moisture readings - all displayed or at the press of a button.
2. Then, depending on the time of day - the arduino turns the sprinklers on and sets a timer going ... blah blah
3. etc etc

If that's the case then the analogous problem is a state machine blinker which might be worth looking at.
https://www.sparkfun.com/news/1801 gives plenty of background or just cut to the chase and use the programming example.
There are many many other examples of this same approach with the arduino as close as google.
You're got a clock - so just check for the time to turn on and the time to turn off. In loop() check the current time against the required times. If they are equal then either start or stop the sprinklers.

Something like:

1
2
3
4
5
if (time == 5am || time == 7pm) && soil moisture >= 560
    then Solenoid Valves = ON

if (time == 5:10am || time == 7:10pm) && Solenoid Valve is ON
   then Solenoid Valves = OFF


Also, you might want to invest in getting a RTC with a back-up battery. That way you don't have to hard code any initial date/time settings in the code as here.

PS Also in printTime(), you might want to save the previous time and only display time if the current time is at least second later than the previous time. Otherwise with now no delay, loop() will continuously enter and show the time even if it hasn't changed.
Last edited on
Topic archived. No new replies allowed.