Greenhouse Irrigation System

Hey guys,

I'm fairly new to arduino, I have made a few projects however I have normally copied the code from other projects, typing them out manually to try and learn the language. However this time I decided to start from scratch on my code, to try and get my head around programming myself. While I managed to get the project to work in a basic principle, I am wanting to upgrade it and make it a little 'smarter'.

This is what I have so far:

const int AirValue = 614;   //you need to replace this value with Value_1
const int WaterValue = 241;  //you need to replace this value with Value_2
int intervals = (AirValue - WaterValue)/3;
int soilMoistureValue = 0;


// Relay pin is controlled with D8. The active wire is connected to Normally Closed and common
int relay = 8;

void setup() {

  // Pin for relay module set as output
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);

  Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop() {
soilMoistureValue = analogRead(A0);  //put Sensor insert into soil
if(soilMoistureValue > WaterValue && soilMoistureValue < (WaterValue + intervals))
{
  Serial.println("Very Wet");
  digitalWrite(relay, HIGH);  
}
else if(soilMoistureValue > (WaterValue + intervals) && soilMoistureValue < (AirValue - intervals))
{
  Serial.println("Wet");
}
else if(soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals))
{
  Serial.println("Dry");
  digitalWrite(relay, LOW);
}
delay(2500);
}

This project uses an Arduino, a relay connected to the pump, and a Capacitive Moisture Sensor V1.2 - And that is it so far!

I set the delay to 2500, to try and save a bit of battery life (rightly or wrongly?), however this is great when the pump is off, hower it seems to take alot longer to turn the pump off again, by which point alot of water has obviously been pumped through. Is there a way to set the delay shorter while the relay is LOW?

I would also like to use a secondary sensor to sense if the water level in the storage container is too low, and stop the relay from running if it is, to save damaging the pump.

Is there also a way that the program doesn't run during the night, then switches back on again on sunrise?

As you can see, I can write a basic code, but add more than one variable and I start to confuse myself! :frowning:

I also have other plans to expand this project, however these are not important at the minute. These include:

  • LCD screen showing hydration level.
  • Temperature sensor to test inside to outside air temperature, and possible control a fan and / or vent
  • Light controller, to really maximise growth

Just to clarify, this is being used for vegetables, and NOT for anything that could be considered illegal!

Thanks in advance guys!
[/list]

Especially if you have plans to add functionality, it would be best to avoid use of delay. Look at the blink without delay example in the IDE and Robin2's "
Demonstration code for several things at the same time" sticky thread at the top of this page.

Looking at your code, you turn on the pump when the soil is measured "dry", and you keep it on until you measure it to be "very wet". When you decrease measurement frequency, it will obviously take longer to measure this new state. I would suggest:

else if(soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals))
{
  Serial.println("Dry");
  digitalWrite(relay, LOW);

  while(!(soilMoistureValue > WaterValue && soilMoistureValue < (WaterValue + intervals))
  {
    soilMoistureValue = analogRead(A0);
    delay(100);
  }
}

This will measure with a higher frequency during the watering.

Also, if you are concerned with power consumption, why are you keeping the relay activated all the time? I would suggest rewiring the pump to the NO of the relay.

As for the other functions you are suggesting:

A level sensor on the tank can done quite easily. You could use another of the same sensors, you could use a load cell, and there are many more ways of doing it.

Running only during the daytime can be done by hooking up a photoresistor in a voltage divider.

However, as wildbill already mentioned, if you are piling more and more functionality onto the Arduino, you want to completely restructure your code. Have a look at the "several things at the same time" sticky. However, this is a little bit more advanced than what you are doing right now. Especially if you're still learning, add stuff one step at a time and get it to work before moving on. You will eventually start exploring the bounds of what your code can do and find ways to stretch them. Thats how we learn (;