Hello everyone. I am building automatic watering system. Each flower pot have moisture sensor. Audrino Uno read analog values from each pot containing the sensor. I wrote the code, which will activate solenoid valve and water pump according to requirements of each flower pot. I am having the problem with my code. My code works like that: when soil moisture sensor measure value below 10% of humidity it activate solenoid valve (each pot have one solenoid) and when it read more than 80% humidity it closes the solenoid. Activating solenoids is not the issue. Then Adruino activate the output to activate relay for water pump. Each pot have it own output for water pump.
I cannot write the code which will use only one output for water pump ( I want that solenoids are controlled over multiple outputs and use this values to start running only 1 water pump). I tried to add int values and use of logic gates but I cannot make the code work properly. I want to add 4 more flower pots in the system. Code is written only for 2 pots. I am beginner and this is my first project. I will appreciate a lot if someone can help me with the code. Im adding my code and photo of my Audrino Uno and breadboard with elements. Thank you.
int soilMoistureValue1 = 0; // soil moisture of the plant pot 1
int soilMoistureValue2 = 0; // soil moisture of the plant pot 2
int percentage1 = 0;
int percentage2 = 0;
void setup() {
pinMode(13, OUTPUT); // control the solenoid valve of pot 2
pinMode(12, OUTPUT); // control the solenoid valve of pot 1
pinMode(2, OUTPUT); // control the water pump of pot 1
pinMode(3, OUTPUT); // control the water pump of pot 2
Serial.begin(9600);
}
void loop() {
soilMoistureValue1 = analogRead(A0); // analog read from moister sensor in the pot 1
Serial.println(percentage1);
percentage1 = map(soilMoistureValue1, 490, 1023, 100, 0);
if(percentage1 < 10)
{
Serial.println("pot1 on");
digitalWrite(12, HIGH); // it open solenoid valve in pot 1
digitalWrite(2, HIGH); // it activate water pump for pot 1
delay(2000);
}
if(percentage1 >80)
{
Serial.println("pot1 off");
digitalWrite(12, LOW); // it close solenoid valve in pot 1
digitalWrite(2, LOW); // it shut off the water pump for pot 1
delay(2000);
}
soilMoistureValue2 = analogRead(A1); // analog read from moister sensor in the pot 2
Serial.println(percentage2);
percentage2 = map(soilMoistureValue2, 490, 1023, 100, 0);
if(percentage2 < 10)
{
Serial.println("pot2 on");
digitalWrite(13, HIGH); // it open solenoid valve in pot 2
digitalWrite(3, HIGH); // it activate water pump for pot 2
delay(2000);
}
if(percentage2 >80)
{
Serial.println("pot2 off");
digitalWrite(13, LOW); // it close solenoid valve in pot 2
digitalWrite(3, LOW); // it shut off the water pump for pot 2
delay(2000);
}
}
// This code is only for test. I am planing to add 4more pots in the code. Same way like first 2 pots
In that case, start over with ONLY the solenoids and write a program to get them to operate properly and how you will control them
Then get ONLY the moisture reading sensors and write a program to test each and all of them.
Only now are you confident to write your final watering program.
Do not start projects from the TOP DOWN.
In general - Arrays and structs are your friends.
Don't duplicate code in your sketch. Write code once - use it multiple times.
You should not use magic numbers. The I/O pins love to have a functional name.
Do you have experience with programming in C++?
The task can easily be realised with an object.
A structured array contains all information, such as pin addresses for the I/O devices, as well as the information for the timing.
A single service takes care of this information and initiates the intended action.
The structured array makes the sketch scalable until all I/O pins are used up without having to adapt the code for the service.
It is cool stuff, isn´t it?
Hello. Basically I'm having written code to calibrate my moister sensors. Opening of solenoids is already working. Like I have mentioned my only problem is water pump control.
Save your code, then chop down a copy to work with one flower pot and one of everything that the flower pot needs, like one sensor and one pump, whatever.
At a glance, the code looks plausible, try to get one section working.
At that point, you can do the cut/paste/edit style of development, which works for a few but as has been said, once you get to N copies, it is easier to take a break and learn about functions and array variables.
Together with loops like for and while, they are the key to leveraging the code so that one copy only is needed, and it can pay attention to each flower pot in turn.
Thank you very much for reply. Actually I have 2 potentiometers connected to analog inputs to simulate moister sensors. I've started with only one in the beginning. The system work. When the value below 10% of humidity is reached audrino open solenoid and activate water pump. When humidity reach 80% system close solenoid but water pump still running because solenoid for second pot is still open (humidity have not reached 80% yet).Of course when level is reached solenoid is closed and water pump stop. I just need the part of code which will use only one output to control pump and not 2 outputs like in my case. I have no experience with C language.
...that pump should be on if any solenoid is active?
You can see if a solenoid valse is open by reading back what your control statements wrote, viz:
if (digitalRead(12) == HIGH || digitalRwsd(13) == HIGH) { // any solenoids?
digitalWrite(pumpPin, HIGH); // yes, turn on pump
}
else {
digitalWrite(pumpPin, LOW); // no, no need to lump
}
|| is the logical or operator. You wanna turn on the pump if the first solenoid or the second solenoid (or both, it is inclusive or) is active.
Pick a pin for the one pump. Pick some names for the other numbers you use for pins. It makes reading the code easier, I heard that somewhere.
HTH
And this is where when you are copy/paste/editing you should instead take a break and switch to learning time and learn about functions and array variables.
Trust us, it will make your life easier, especially if you need to correct, modify or enhance the 5x copies of what is near-identical code.
Thank you for your reply. Yes if any solenoid is active pump should run. Yes I agree there will be a lot of learning from my side. I'm just getting to know how the C++ language works. It's my first small project. I started like week ago. I will adjust my code and change names of the pins. It's like you said, much easier to understand the code. Thank you again for your help. Much appreciated.
If I understand properly digitalRead(12) == HIGH || digitalRwsd(13) == HIGH) { // any solenoids?
digitalWrite(pumpPin, HIGH); // yes,.
DigitalRead(12) is now input right?
This logical function can only apply when I have 2 variables. I should use arrays when I have 3 or more variables?
@alto777@dden1s Yeah indeed we all have to start learning how to deal with functions, commands, private or public classes and so on and indeed for a beginner it´s not easy. I´m used to program in other languages but Arduino isn´t that simple of course it´s C++ based but somewhat different
I was planning that after watering will work I'll go in safety. I was planning to add water detector to the system. So if there is a leaking, sensor will shut off the water pump.