Automated Irrigation using RTC, Capacitive Soil Moisture Sensor, 3v Solenoid Valves

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

My code is as follows:
#include <Wire.h>
#include "DS1307.h"

DS1307 clock;//define a object of DS1307 class
const int SOLENOID = 13;
int sensorValue = analogRead(A0);
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();
{
switch(clock.hour){
case 5:
if (sensorValue>560){
digitalWrite(SOLENOID, HIGH);
delay(600000);
digitalWrite(SOLENOID, LOW);
delay(3600000);
}
case 19:
if (sensorValue>560){
digitalWrite(SOLENOID, HIGH);
delay(600000);
digitalWrite(SOLENOID, LOW);
delay(3600000);
}
}
}
}
/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(" ");
}

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.

Hello
Well, I think the delay() funktion will block the execution of the program.

1 Like

Thanks. What commands could I try instead?

Hello
why do you need these delay()´s ?

I want the valves to stay open for 10mins

you can use a timer funktion by using the millis()´s function.

https://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html

Is this what you're referring to?
I can kinda understand the concept, but I'm not sure how to express it in code.

I think I've got the gist of it. However, is it possible to time delays using the RTC instead of millis?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.