Where to put the code to make the system water each plant for 30 seconds, and then shut off the corresponding Relay.
int IN1 = 2;
int IN2 = 3;
int IN3 = 4;
int IN4 = 5;
int Pin1 = A0;
int Pin2 = A1;
int Pin3 = A2;
int Pin4 = A3;
float value1 = 0;
float value2 = 0;
float value3 = 0;
float value4 = 0;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(Pin1, INPUT);
pinMode(Pin2, INPUT);
pinMode(Pin3, INPUT);
pinMode(Pin4, INPUT);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
delay(500);
}
void loop() {
Serial.print("MOISTURE LEVEL:");
value1 = analogRead(Pin1);
Serial.println(value1);
if(value1>550)
{
digitalWrite(IN1, LOW);
}
else
{
digitalWrite(IN1, HIGH);
}
Serial.print("MOISTURE LEVEL:");
value2 = analogRead(Pin2);
Serial.println(value2);
if(value2>550)
{
digitalWrite(IN2, LOW);
}
else
{
digitalWrite(IN2, HIGH);
}
Serial.print("MOISTURE LEVEL:");
value3 = analogRead(Pin3);
Serial.println(value3);
if(value3>550)
{
digitalWrite(IN3, LOW);
}
else
{
digitalWrite(IN3, HIGH);
}
Serial.print("MOISTURE LEVEL:");
value4 = analogRead(Pin4);
Serial.println(value4);
if(value4>550)
{
digitalWrite(IN4, LOW);
}
else
{
digitalWrite(IN4, HIGH);
}
Serial.println();
delay(1000);
}
The easier you make it to read and copy the code the more likely it is that you will get help
Please follow the advice given in the link below when posting code , use code tags and post the code here
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frustrating for you and frustrating for us.
The people who try to help with your pro…
Suppose that each relay is turned on for 30 seconds as you want, then turns off. Should the relays turn on again if the moisture value is still above the threshold ? If so, then why not just leave them on while the threshold is exceeded ?
HI Robson,
you should improve your code in two aspects.
Pressing Ctrl-T in the Arduino-IDE to do autoformatting the indentions.
Which will make your code much easier readable.
Using self.explaining names for everything.
Using The letters "IN1" for an O utput is really strange.
Using Pin1 as a symbolname is saying nothing at all.
Yes thery are all IO-pins but what is the purpose of pin1?
In your case with mutliple sensors and relays doing all the same this might be somehow tolerable but it is regarded as bad programming-style.
So my suggestion is:
int Valve1 = 2;
int Valve2 = 3;
int Valve3 = 4;
int Valve4 = 5;
int moistureSensor1_Pin = A0;
int moistureSensor2_Pin = A1;
int moistureSensor3_Pin = A2;
int moistureSensor4_Pin = A3;
int moistureSensor1 = 0;
int moistureSensor2 = 0;
int moistureSensor3 = 0;
int moistureSensor4 = 0;
const int moistureThreshold = 550;
void setup() {
Serial.begin(9600);
pinMode(Valve1, OUTPUT);
pinMode(Valve2, OUTPUT);
pinMode(Valve3, OUTPUT);
pinMode(Valve4, OUTPUT);
pinMode(moistureSensor1_Pin, INPUT);
pinMode(moistureSensor1_Pin, INPUT);
pinMode(moistureSensor1_Pin, INPUT);
pinMode(moistureSensor1_Pin, INPUT);
digitalWrite(Valve1, HIGH);
digitalWrite(Valve2, HIGH);
digitalWrite(Valve3, HIGH);
digitalWrite(Valve4, HIGH);
delay(500);
}
void loop() {
Serial.print("MOISTURE LEVEL:");
moistureSensor1 = analogRead(moistureSensor1_Pin);
Serial.println(moistureSensor1);
if (moistureSensor1 > moistureThreshold)
{
digitalWrite(Valve1, LOW);
}
else
{
digitalWrite(Valve1, HIGH);
}
etc. etc.
best regards Stefan
Hello
Do you have experience in the usage of data structs using the struct{} instruction?
If not, than take some time to look into. That will save a lot of line of codes and time to debug.
In short words: Using the struct{} instruction you will build an object contain the information about the used valve, moisture sensor and timer to do the requested job.
Do yourself the favor, you will learn a lot.
Since all of the valves and sensors do the same thing, it is an excellent opportunity to use some basic arrays:
const byte ValveCount = 4;
const byte ValvePins[ValveCount] = {2, 3, 4, 5};
const byte MoistureSensorPins[ValveCount] = {A0, A1, A2, A3};
const int MoistureThreshold = 550;
void setup()
{
Serial.begin(9600);
for (int valve = 0; valve < ValveCount; valve++)
{
pinMode(ValvePins[valve], OUTPUT);
digitalWrite(ValvePins[valve], HIGH);
}
delay(500);
}
void loop()
{
// For each of the watering areas
for (int valve = 0; valve < ValveCount; valve++)
{
int moistureLevel = analogRead(MoistureSensorPins[valve]);
Serial.print("MOISTURE LEVEL:");
Serial.println(moistureLevel);
// If the area is too dry
if (moistureLevel > MoistureThreshold)
{
// Turn on the water for thirty seconds
digitalWrite(ValvePins[valve], LOW);
delay(30000);
digitalWrite(ValvePins[valve], HIGH);
}
}
}
Thanks for the code, now I just got to add a pump which will in turn switch on when any of the sensors are triggered.
much appreciated
Robi
To turn on a pump when water is needed:
{
// Turn on the water for thirty seconds
digitalWrite(ValvePins[valve], LOW);
digitalWrite(PumpPin, LOW);
delay(30000);
digitalWrite(PumpPin, HIGH);
digitalWrite(ValvePins[valve], HIGH);
}
Thank you, will try it out soon.
Very much Appreciated.
system
Closed
September 13, 2021, 3:09pm
#10
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.