there are several conditions to turn on water pump using relay, conditions are as follows
Time (DS3231) municipal water supply are on timely basis 5.30 to 8.30 am & 4.00 to 8.00 pm
temp of motor should be <63 (DS18B20) as mentioned in motor manual
psi (pressure transducer) min & max defined to protect motor from dry run and over pressure
Ultrasonic sensor (A02YYUW) min & max level to turn on & off auto
Soil moisture sensor (motor installed outside to protect from rain)
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <DS3231.h>
RTClib myRTC;
DateTime now = myRTC.now();
#define Relay1 22
// ds18b20
#define ONE_WIRE_BUS 37
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Soil moisture sensor
#define soilSensorPin A11
float soilsensorValue = 0;
// Ultrasonic sensor calc
const int trigPin = 26;
const int echoPin = 27;
int level;
long duration;
int distance;
float water_percentage;
int water_bottom = 160;
int water_top = 25;
int water_distance = 135;
float sensor = 0;
// Pressure transducer
const int pressureInput = A10;
const int pressureZero = 102.4;
const int pressureMax = 921.6;
const int pressuretransducermaxPSI = 100;
const int sensorreadDelay = 250;
float pressureValue = 0;
void setup() {
Serial.begin(9600);
sensors.begin();
Wire.begin();
pinMode(Relay1, OUTPUT);
digitalWrite(Relay1, HIGH);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
motorState();
}
void soilSensor() {
for (int i = 0; i <= 100; i++)
{
soilsensorValue = soilsensorValue + analogRead(soilSensorPin);
delay(1);
}
soilsensorValue = soilsensorValue / 100.0;
Serial.println(soilsensorValue);
delay(30);
}
void usSensor() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
water_percentage = (100 * (water_top + water_distance - distance)) / water_distance;
level = water_percentage;
sensor = water_percentage;
Serial.print("wLevel: ");
Serial.println(sensor);
delay(250);
}
void temp() {
sensors.requestTemperatures();
Serial.print("temp: ");
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
}
void psi() {
pressureValue = analogRead(pressureInput);
pressureValue = ((pressureValue - pressureZero) * pressuretransducermaxPSI) / (pressureMax - pressureZero);
Serial.print("psI: ");
Serial.println(pressureValue, 1);
delay(250);
}
void motorState() {
if ((now.hour() >= 5.30 && now.hour() < 8.30) && (now.hour() >= 16.00 && now.hour() < 20.00))
Serial.println(now.hour());
{
if ((psi > 60 && psi < 120) && (temp < 63) && (soilsensorValue < 100)) {
digitalWrite(Relay1, LOW);
Serial.println("Motor On");
}
else {
digitalWrite(Relay1, HIGH);
Serial.println("Motor Off");
}
}
}
LarryD
September 4, 2022, 3:50am
2
aadi2910:
if ((now.hour() >= 5.30
Tell us what this says ?
What does this return ? now.hour()
Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
blh64
September 4, 2022, 4:00am
3
It is much , much easier to just deal with minutes since midnight. If your RTC is configured to report in 24 hour format, calculate the current minute
int currentMinute = now.hour() * 24 + now.minute();
and then do your time comparison
If your RTC is configured in 12 hour mode (am/pm) then you will have to adjust.
1 Like
alto777
September 4, 2022, 1:36pm
4
Understandable, I assume. And your code looks a bit creative, but plausible. There are some obvious mistakes, already hinted at. now.hour () is an integer, comparing it to a floating point number, e.g. 5.3, will probably not be doing what it looks like you want.
Do you have a question?
Or observations you can share? What does the code do that it should not? What does the code not do that it should?
a7
gcjr
September 4, 2022, 1:57pm
5
consider the following approach
const byte PinMotor = LED_BUILTIN;
int debug;
int hrMin;
const int DegCmax = 63;
int degC;
const int PsiMin = 20;
const int PsiMax = 80;
int psi;
const int SoilMin = 5;
const int SoilMax = 10;
int soil;
int ultra;
enum { Off = HIGH, On = LOW };
// -----------------------------------------------------------------------------
// process single character commands from the PC
void
pcRead (void)
{
static int val = 0;
if (Serial.available()) {
int c = Serial.read ();
switch (c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
val = c - '0' + (10 * val);
break;
case 'D':
debug ^= 1;
break;
case 'd':
degC = val;
val = 0;
break;
case 'm':
digitalWrite (PinMotor, val);
val = 0;
break;
case 'p':
psi = val;
val = 0;
break;
case 's':
soil = val;
val = 0;
break;
case 't':
hrMin = val;
val = 0;
break;
case 'u':
ultra = val;
val = 0;
break;
case '.':
Serial.print (" hrMin "); Serial.println (hrMin);
Serial.print (" psi "); Serial.println (psi);
Serial.print (" degC "); Serial.println (degC);
Serial.print (" soil "); Serial.println (soil);
Serial.print (" ultra "); Serial.println (ultra);
break;
case '?':
Serial.println ("\npcRead:\n");
Serial.println (" [0-9] append to #");
Serial.println (" #d - set degC");
Serial.println (" #p - set psi");
Serial.println (" #s - set soil");
Serial.println (" #t - set hrMin");
Serial.println (" #u - set ultra");
Serial.println (" ? - list of commands");
break;
default:
Serial.print ("unknown char ");
Serial.println (c,HEX);
break;
}
}
}
// -----------------------------------------------------------------------------
void
motorOn (
bool on)
{
Serial.print ("motorOn: ");
Serial.println(on);
digitalWrite (PinMotor, on);
}
// -----------------------------------------------------------------------------
bool
motorOnTime (void)
{
return (530 < hrMin && hrMin < 830) || (1600 < hrMin && hrMin < 2000);
}
// -----------------------------------------------------------------------------
bool motorPsiOk (void) { return PsiMin <= psi && psi <= PsiMax; }
bool motorTempOk (void) { return degC < DegCmax; }
bool soilOk (void) { return SoilMin <= soil && soil <= SoilMax; }
// -----------------------------------------------------------------------------
void
loop (void)
{
if (On == digitalRead (PinMotor)) {
if (! motorOnTime () || ! motorTempOk () || ! motorPsiOk ()
|| ! soilOk ())
motorOn (Off);
}
else {
if (motorOnTime () && motorTempOk () && motorPsiOk ()
&& soilOk ())
motorOn (On);
}
pcRead ();
}
// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (9600);
pinMode (PinMotor, OUTPUT);
digitalWrite (PinMotor, Off);
Serial.println ("ready");
}
system
Closed
March 3, 2023, 1:58pm
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.