# How to code multiple conditions inside if statement?

I have a system that checks the value of a moisture sensor and will also check the time

if (t.hour == 7 && t.min == 00 && moistureOneSensorValue >= 700) {
RelayOne();
RelayTwo();
}

When moistureOneSensorValue is greater than or equal to 700 it means the soil is DRY, so when all conditions inside the if statement are true the relayOne and relayTwo functions will be called.

My problem is that when moistureOneSensorValue is still moist the above if statement will result in false and the relayOne and relayTwo functions will not be called. I want Arduino to execute and call the relayOne and relayTwo functions and disregard the time. I use time so that the system will only call the relayOne and relayTwo functions in the morning at exactly 7 AM every day, but it needs to check/make sure that the soil is DRY...

Example

First thing in the morning the system will check the time and sensor value; if the if statement results in false the system will keep monitoring and try the if statement until the soil is DRY. But I think when that happens it will be over 7 AM.

Sorry for the messy explanation.

• I think where is says "s still moist" in your question you meant to say "s still dry". May 15 '17 at 13:19
• yeah , when it still dry the if statement will result in true .. May 15 '17 at 13:23
• You first have to sort out in your head what you want the Arduino to do. At this point it just seems you do not know what you want. May 15 '17 at 13:51

I'm not sure how quickly you soil could get dry, but I suspect what you want to do is to periodically check the dryness throughout the day. Say like every two hours:

// Check every 2 hrs (when hr is even)
if ( t.hour % 2 == 0 && moistureOneSensorValue >= 700 ) {...}

You could also bound it to only during the day time:

if ( t.hour >= 7 // after 7a
&& t.hour <= 19 // before 7p
&& t.hour % 2 == 1 // every 2 hrs (when hr is odd)
&& moistureOneSensorValue >= 700 // soil is dry
) {...}

If the event is to be time independent then it follows you can not check the time. If the event is to be both time independent and dependent then you need to add the code to handle the logic.

Consider checking if it is between 7am and, say, 7pm. If true then test if the moisture detector is dry. If true then close (watering) relays.

(This is only a code example and has not been tested.)

if(t.hour >= 7 && t.hour <= 19)
{
while(moistureOneSensorValue >= 700)
{
RelayOne();
RelayTwo();
}
}

Consider adding a state machine to mitigate possible problems by monitoring minimum times between events and maximum times of events. For example, do not water more than once per 5 minutes. Do not water more than 30 seconds at a time. Do not water more then a total of 1 minute per hour. Do not water more than 5 minutes per 24 hours.

• So i just code like this , if (t.hour == 7 && t.min == 00 && ) { while(moistureOneSensorValue >= 700) RelayOne(); RelayTwo(); } May 15 '17 at 13:38
• An untested code example has been added to the answer. May 15 '17 at 14:00
• what if the soil is quickly dry ? so it will water the plant more than the allowed watering a day May 15 '17 at 14:01
• Those were examples times and constraints. If I were to make an automated watering system I would be very very worried about water damage should something go wrong. Maybe you will have a fix amount of water such as a watering jug as your water source so as to avoid the problem. Maybe you will only do this out side where watering problems will do less damage. Maybe you need to enhance your software such that it will mitigate over or under watering problems. Maybe you are watering a 10 ounce potted plant & need to keep the times short. Maybe you are watering a plot of tomato vines. May 15 '17 at 14:07
• I'm just saying, there are a lot of open ended problems for such a project. Software can offer the flexibility to solve most of them. But you need to think each and every problem through. May 15 '17 at 14:10