Yup you guessed it im new.... Now that we got that out of the way.
*** I want to water my plants after dusk if the soil is dry.
I want my plants to be watered about every two to three days, this will be determined by the soil moisture. The 120VAC relay will only come on after dusk to avoid the plants being watered in the heat of the day. This whole idea is simple to me but getting it sketched seems to be my ultimate issue.
I can write all the basic codes such as turn a relay on or turn the LED on when the sun goes down but I am having issues with the if statements. Any help would be greatly appreciated.... if you don't plan on helping or saying something stupid like use the search button, take a hike. I am here looking for somewhat of a mentor for this first project.
The most useful thing that you can do is to post your code, including the if statements that you are having trouble with and explain what is going wrong.
Does this look ok to everyone? Do I have room for improvement?
// This sketch will run a 120v Relay to turn a pump
// on when the soil moisture is low and after dusk.
int moistureSensor = 0;
int lightSensor = 1;
int moisture_val;
int light_val;
void setup() {
Serial.begin(9600); //open serial port
pinMode (2, OUTPUT);
digitalWrite (2, LOW);
}
void loop() {
moisture_val = analogRead(moistureSensor); // read the value from the moisture sensor
Serial.print("moisture sensor reads ");
Serial.println( moisture_val );
if (moisture_val < 800 && light_val < 600)
{
Serial.println("turning off pump");
digitalWrite (2, LOW);
delay(2000);
}
if (moisture_val > 800 && light_val > 600)
{
Serial.println("turning on pump");
digitalWrite (2, HIGH);
delay(2000);
}
light_val = analogRead(lightSensor); // read the value from the photosensor
Serial.print("light sensor reads ");
Serial.println( light_val );
if (light_val < 600)
{
Serial.println("turning off LED");
digitalWrite (13, HIGH);
delay(2000);
}
if (light_val > 600)
{
Serial.println("turning on LED");
digitalWrite (13, LOW);
delay(2000);
}
}
I forgot to remove the write LED on off, this was for my final desk check. Everything seems to be working but again, does anyone see anything that can be improved?
I would start the loop() function with a check of the light level rather than the moisture and do nothing if it is not dark. Then if it is dark do the moisture check, start watering if it is dry and stop watering when it is wet enough. That way you do not have to check the light level elsewhere the program.
To improve readability of the code I would give the pins meaningful names either by using #define or by using const int named variables. If at some point you want to incorporate other inputs, perhaps a manual override to stop watering or a temperature sensor perhaps, then you would be wise to avoid using the delay() function because it blocks program operation. Instead use the principle shown by the BlinkWithoutDelay example in the IDE to note the start time of a state and check periodically if the required elapsed time has passed.
once you get the unit to work, I would offer that some empirical testing would help performance.
turn on the water and wait until the moisture reading increases to either a value or percentage, then stop watering.
watch the value increase as the moisture reaches and equilibrium in the soil. although there are few plants that cannnot tolerate over watering, it would be expected that the actual moisture will continues to rise after the water is turned off and the water penetrates the soil.
it may be that running the water for x seconds will yield certain value.
if you add a data logger, then I would want the soil moisture reading throughout the day. To see about when the water level reached a threshold.
I suspect that will result in over-watering since it will take significant time for water to disperse through the soil. Better IMO to use the dryness measurement to decide how much water to apply. In effect it means that the moisture level is being controlled over the daily cycle rather than in real time during the watering loop.
xXElusiveXx:
Could you give me an example of what that would look like?
Here is some pseudo code to give you a start
start of loop()
//code to detect light and soil moisture
if watering is needed
//commands to start watering
startedWatering = millis();
end of if
if millis() - startedWatering >= waterinfPeriod
//commands to stop watering
end of if
end of loop()