hydroponic garden

So I'm new at this, I have a hydroponic garden that i would like to automate, here's what i have but i need some help with the coding please!

I have 8 5vc coil relays with 250/120vac contacts
external 5vdc power supply for relays
Arduino Uno connected via usb 2.0 to raspberry pi 4B 4GB Ram
3.2 or 3.5" TFT display

Important note: the relays are working on Inverted logic, meaning High or 1 is Off and Low or 0 is on**

// D3 = Blue Led indicator for Grow Lights
// D4 = Yellow Led indicator for Air Pump
// D5 = Red Led indicator for Sump Pump
// D6 = Relay 1 = Grow Lights
// D7 = Relay 2 = Intake ("Exhaust Fan")
// D8 = Relay 3 = Exhaust ("Exhaust Fan")
// D9 = Relay 4 = Air Pump
// D10 = Relay 5 = Sump pump ( waters plants)
// D11 = Relay 6 = Recirculation Fan
// D12 = Relay 7 = Heating lamp (Future Use - During Winter)
// D13 = Relay 8 = 5Vdc Power Source
// A0 = DH11 Temp. and Humidity Sensor
// A1 = Soil Moisture sensor

I have some of the program done, but i think i would like it different as I'm using delays in milliseconds for 16 hours not quite sure it will function properly, so here's what i would like if anyone is interested in helping:

in void loop()
real time without adding any extra circuitry to turn relayPin1 on for 16 hrs
real time to turn relayPin1 back off for 8 hours

time interrupts every 10 seconds to check for temp if 26 degree Celsius or 80 degrees Fahrenheit, turn on relay pins 2 and 3, and if below 21.11 Celsius or 70 degrees Fahrenheit turn off relay pins 2 and 3. Would also like this to display on the tft screen attached to the raspberry pi 4b.

would like the soil moisture sensor to read every hour with a limit of 100 and if the limit is less than or equal to 20 and greater than or equal to 11, to turn on relay pin 4 print out Soil moisture at 20 percent turn air pump on and display on tft, as well as turn on the yellow led indicator light.

if soil moisture is less than or equal to 10 percent then turn on relay pin 5 sump pump and red led indicator light. print to tft screen on raspberry pi.

if soil moisture is greater than or equal to 21 and less than or equal to 80 print soil moisture is in normal range to the tft connected to raspberry pi. Turn off relay pins 4 and 5 and indicator leds.

else soil moisture is greater than or equal to 81 wait two hours to check moisture again and print Soil moisture is way too high to tft screen on raspberry pi. Turn off relay pins 4 and 5 and indicator leds.

Recirculation Fan Relay pin i would like to turn off and on every 30 mins, but only while grow lights are on, so would need to be done with real time, and print on tft when on and when off

The 5vdc on Relay 8, is to externally power cooling fans for Raspberry Pi, and Arduino based off of the temp. of the raspberry pi processor in which i would also like to print to the tft screen as well.

Any help with this would be greatly appreciated, as i expect to start a new job and won't have much time to complete after today. With Raspberry pi running two 32" monitors, until program is finished i needed the external power to run relays and fans or had power issues, even with the 5.1 3.2A factory power supply.

forgot to mention that the blue indicator led should come on simultaneously with grow lights. Any help is appreciated.

// most of my code, it does contain errors, hence forth the help I’m asking for:

#include <Serial.h>

#include <DHT.h>

Led1, 3;
Led2, 4;
Led3, 5;
Relay1, 6;
Relay2, 7;
Relay3, 8;
Relay4, 9;
Relay5, 10;
Relay6, 11;
Relay7, 12;
Relay8, 13;
DHT.dht A0;
SensorValueA1;
int A = 0;
int B = 0;

void setup()
Serial.begin(9600);
pinMode(Relay1, OUTPUT); // Grow lights
pinMode(Relay2, OUTPUT); // Intake “Exhaust Fan”
pinMode(Relay3, OUTPUT); // Exhaust “Exhaust Fan”
pinMode(Relay4, OUTPUT); // Air Pump
pinMode(Relay5, OUTPUT); // Sump Pump
pinMode(Relay6, OUTPUT); // Recirculation Fan (Future Use awaiting fan)
pinMode(Relay7, OUTPUT); // Heating Lamp (Future Use During Winter)
pinMode(Relay8, OUTPUT); // External 5vdc for Raspberry pi and Arduino Fans
relayState1 = HIGH; // relayState
pinMode(5, OUTPUT); // Red led indicator light for sump pump
pinMode(4, OUTPUT); // Yellow led indicator light for Air pump
pinMode(3, OUTPUT); // Blue led indicator light for Grow lights
digitalWrite(Relay1, HIGH); // Inverted logic relays High is off so setting relays to off
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);
digitalWrite(Relay5, HIGH);
digitalWrite(Relay6, HIGH);
digitalWrite(Relay7, HIGH);
digitalWrite(Relay8, HIGH);
delay(1000); // Delay to let system boot
Serial.println(“DHT11 Humidity and Temperature Sensor”); // DHT 11 sensor
delay(1000); // Wait before accessing sensor
} // end “setup()”

void loop()
// Start of Program
DHT.read11(dht_apin);

Serial.print(“Current humidity”);
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print(DHT.temperature = ");
Serial.println(“C”);
Serial.print((DHT.temperature * 1.8) + 32);
Serial.println(“F”);
delay(10000) // 10s delay in milliseconds
Serial.println(AnalogValue : ");
Serial.println(sensorValue);

if (sensorValue <= 20) {
digitalWrite(4, HIGH); // Yellow Led indicator light for Air Pump
digitalWrite(Relay4, LOW); // Inverse Relay logic, power on Relay 4 on (Air Pump)
Serial.print(“Air Pump On”);
delay(3600000) // 60601000
digitalWrite(5, HIGH); // Red Led indicator light for Relay 5 on (Sump Pump)
digitalWrite(Relay5, LOW); // Inverse Relay logic, power on Relay 5 (Sump Pump)
Serial.print(“Sump Pump On”);
delay(1800000) // 60301000
digitalWrite(4, LOW); // Yellow Led indicator light for Air Pump off
digitalWrite(5, LOW); // Red Led indicator light for Air Pump off
digitalWrite(Relay4, HIGH); // Inverse Relay logic, power off Relay 4 off (Air Pump)
digitalWrite(Relay5, HIGH); // Inverse Relay logic, power off Relay 5 off (Sump Pump)
Serial.println(“Air Pump and Sump Pump Off”);
}
else {
digitalWrite(4, LOW); // Yellow Led indicator light off
digitalWrite(5, LOW); // Red Led indicator light off
digitalWrite(Relay4, HIGH); // Inverse Relay logic, power off Relay 4 (Air Pump)
digitalWrite(Relay5, HIGH); // Inverse Relay logic, power off Relay 5(Sump Pump)
delay(3600000) // 60 * 60 * 1000
}
if (DHT.temperature >= 26) {
Serial.println(“Temperature too HIGH, turn on the Exhaust Fans!”);
digitalWrite(2, LOW); // Inverse Relay logic, power on Relay 2 (Intake Exhaust Fan)
digitalWrite(3. LOW); // Inverse Relay logic, power on Relay 3 (Exhaust, Exhaust Fan)
}
if (DHT.temperature <= 21.11) {
Serial.println(“Temperature too LOW, turn off the Exhaust Fans!”);
digitalWrite(2, HIGH); // Inverse Relay logic, power off Relay 2 (Intake Exhaust Fan)
digitalWrite(3, HIGH); // Inverse Relay logic, power off Relay 3 ( Exhaust, Exhaust Fan)
}
if (DHT.temperature >= 21.11 && DHT.temperature <= 25.99) {
Serial.println(“Temperature in Range”);
digitalWrite(Relay2, 1); // Inverse Relay logic, power off Relay 2
digitalWrite(Relay3, 1); // Inverse Relay logic, power off Relay 3
}
// Fastest possible DHT Read should be every 2 seconds, i don’t need it to be that fast

if (int A==0); {
digitalWrite(Relay1, 0); // Illuminate Grow lights
digitalWrite(3, 1); // Blue Led indicator light for grow lights
Serial.println(“Grow lights Illuminated, ‘Feed me Seymour’!”);
}
long delay (52600000); // 16 hr delay in milliseconds
if (int B ==1); {
digitalWrite(Relay1, 1); // De-Luminate Grow lights
digitalWrite(3, 0); // De-Luminate Blue indicator light
Serial.println(“Grow lights Off, plants are sleeping shhh!!!”)
}
delay long(26800000); // 8 hours in milliseconds
} // end loop()

// this wasn’t the entire program, but you can see why i would like some help

It's a common mistake to write lots of code that doesn't work. You have the extra problem of course that that code doesn't compile either.

I suggest that you make a copy of that code and delete most of it and get something that will compile. Then start testing and add things a piece at a time as the tests prove out. Big bang testing is a very painful approach.

Your variable names could use some work. Why Relay1 when it could be GrowlightsRelayPin? There are loads of similar name changes you can make to produce more comprehensible code.

A couple of fixes:

Functions like setup need an opening brace i.e. { as well as a closing one.

Serial.h doesn't exist and isn't required. Remove the include.

Hi lashworth,

I’m afraid this is going to be a lot more work than you bargained for. I can’t help you on the RTC front, I’m just starting to look into that myself.

I can tell you though that the “delay()” function isn’t going to work for you. AFAIK, delay() prevents the program from progressing the loop until the delay is complete. So for instance, when you delay for 16 hours, it won’t be able to do anything else until that 16 hour window is up, which is far from ideal.

You can use the millis() function to time and trigger events. It will be accurate enough for your needs with the caveat that if the board is reset, the millis function is reset, so you would have to add a value on reboot depending on what time you start up the board.

Here’s a couple of threads to get you started

millis() Beginners

Several things at the same tme

yeah i figured as much that's why i was looking into doing the 16 hour and 8 hour in main loop and do interrupts for the rest which is the part i would need help for, most of this code actually works, i didn't post all of it, the only part that errors is the end with the integers, everything else compiles, again the entire code isn't posted, just enough to give you a general idea of what i'm doing, and where my errors are, looking into changing it to do interrupts for the rest of the program outside of lighting, this way the rest of the program will run during the 16 hr and 8 hour delays. The temperature could do an inerrupt every 5-10 minutes, and the soil moisture could do interrupts based off of the reading of moisture, and just interrupt when it's below the values intended. I'm just not sure how to do the interrupts to change this.

most of this code actually works

Have you tested it? Just because something compiles (and wildbill pointed out that it wont) doesnt mean it does what you want it to do.

Im pretty new to the electronics game too but I've been getting advice on this site long enough to know that you want to be using millis() for timing.

Hopefully someone will be along shortly to tell you why you don't use interrupts like that (I'd be curious to know too).

my best bet is to use a rtc and set the outputs according to the time was just trying to do it without buying anything else until my new job starts, was suppossed to start a new job 2 weeks ago, but the day before i got a better offer, however the better offer hasn't started yet, so buying an rtc until i start getting paid again is out of the question. I wonder if there is a way to import time from the computer via serial cable and then set the outputs.

Found the answers i was looking for here :

under this post https://forum.arduino.cc/index.php?topic=97455.0,
I then found the libraries and examples i need to fix my code, thanks for the replies, when i complete the project i will post final code for those interested.