If statements...

I think this shoudl work, but away from my boards right now.

I have always used the same subject variable (the one getting checked if less than, greater than or equal to) so having if conditions with different variables is new to me)

   if (rtcTimeSeconds >= moistureBoostCommenced) //
    {
      Do this.....
    }
    else if (moistureBoostCommenced == 108000;
    {
      Do this
    }
    else // this will run when the rtc is less than the previous time (so its a new day) and moistureBoostCommenced is not equal to 108000
    {
      tempMoistureBoost = 0;
    }
    else if (moistureBoostCommenced == 108000;

Two problems there: there's no closing bracket ()) and putting the semicolon (:wink: there will end the if statement right there - the block ({}) below will always be executed.

Did you a question?

MorganS:
Two problems there: there's no closing bracket ()) and putting the semicolon (:wink: there will end the if statement right there - the block ({}) below will always be executed.

Did you a question?

Thanks. I copied and pasted before I compiled the code. All fixed now.

The question is probably better done through a basic example. Have I got the solution correct for the code shown (C is the solution).

Cases for code below:
if A = 15 and B = 30, then C = 1.
if A = 25 and B = 30, then C = 2.
if A = 5 and B = 30, then C = 3
if A =5 and B=5, then C = 4.

if (A>10)
{
C = 1;
}
else if (A>20)
{
C = 2;
}
else if (B>20)
{
C = 3;
} 
else
{
C = 4;
}

20 is greater than 10.

MorganS:
20 is greater than 10.

I realise that, but will it return C =1 as that first statement is true? Or will the conditional statement continue to C>20, even if its true for the first condition of A>10?

How about the A = 5 and B = 30, will it return C = 3? I am assuming so, even though the statement is now only checking for values of B in the same conditional block as the comparison of value of A. but curious.

I know this is is easily checked, but I'm not around one to check right now and well.... I guess I am inpatient as I can write some code today (just not check it)

Pete-Repeat:
I realise that, but will it return C =1 as that first statement is true? Or will the conditional statement continue to C>20, even if its true for the first condition of A>10?

It will be C = 1 since A>10. Since that one is true and the second one is an else if the second one never happens. There is no situation where you could end up with C=2. That second if might as well be deleted.

Pete-Repeat:
How about the A = 5 and B = 30, will it return C = 3? I am assuming so, even though the statement is now only checking for values of B in the same conditional block as the comparison of value of A. but curious.

Yes it will. Since A is less than 10 it goes to the second one. A is less than 20 (of course if it is less than 10) so we go to the next one. B is greater than 20 so we stop there with C=3.

May be easier to see without the else if

if (A > 10) { \A=5 so NO
C = 1;
} else {
if (A > 20) {\A=5 so NO
C = 2;
} else {
if (B > 20) {\B=30 so YES
C = 3;
} else {
C = 4;
}
}
}

Pete-Repeat:
Cases for code below:
if A = 15 and B = 30, then C = 1.
if A = 25 and B = 30, then C = 2.
if A = 5 and B = 30, then C = 3
if A =5 and B=5, then C = 4.

There are 3 situations involving B = 30 so deal with them first

if (B == 30) {
  if (A == 5) {
     C = 3;
   }
   else if (A == 15) {
     C = 2;
   }
   else if (A == 25) {
     C = 1;
   }
}
else if (B == 5) {
   if (A == 5) {
      C = 4;
   }
}

...R

Thanks. Oh Yeh should have had A > 10, else of A < 30...

That was just an example to make sure I understood the process, as the one I wrote in my program was harder to explain.

Thanks. It's clear now.

Pete-Repeat:
Oh Yeh should have had A > 10, else of A < 30...

With a different requirement you may have got a different suggestion.

Programming requires great precision.

...R

if A = 15 and B = 30, then C = 1.
if A = 25 and B = 30, then C = 2.
if A = 5 and B = 30, then C = 3
if A =5 and B=5, then C = 4.

Whilst, as Robin has shown, it is possible to use a single test for B == 30, the simplest way to write the code to meet the requirement would be to do exactly what it says. I assume that the values of A and B are being tested (==) rather than being assigned (=)

if (A == 15 and B == 30) C = 1;
if (A == 25 and B == 30) C = 2;
if (A == 5 and B == 30) C = 3;
if (A ==5 and B==5) C = 4;

C programmers with a more traditional background would use && instead of and in the tests and the use of else/if would be preferable to avoid unnecessary code being executed. Personally I would put the code to be executed in { } on its own lines but the single statement on the same line, as above, works

You might also want to think about setting C to a default value, perhaps 0, if all of the tests return false

UKHeliBob:
Whilst, as Robin has shown, it is possible to use a single test for B == 30,

It was in my mind that the example might be an over-simplification so I sought to show a methodology that could be applied more generally.

...R

sought to show a methodology that could be applied more generally.

Quite right that you should. I was not suggesting that your solution was not appropriate, but as we both know it is difficult to hit the right level when answering questions or making suggestions as to how to solve a problem.

The "prime directive", I believe, should be to write code that meets the specification and that the user can understand. For instance, it did occur to me to use the ternary if/else in the solution (maybe even to nest them) but I thought better of it, and I even decided to use and instead of && and to mimic the layout of the specification even though I would not normally lay it out that way.

What was unusual was to get such a clear statement of the requirement.

UKHeliBob:
What was unusual was to get such a clear statement of the requirement.

Yes and No - see Reply #8 :slight_smile:

It has also occurred to me that it may be possible to calculate the output without any need for IF statements. It looks like there may be a simple mathematical relationship between the input and the output.

...R

it may be possible to calculate the output without any need for IF statements. It looks like there may be a simple mathematical relationship between the input and the output.

That would be neat, but beware of what the OP said in reply #8

UKHeliBob:
That would be neat, but beware of what the OP said in reply #8

Which is why I did not take the idea further. And the maths might actually take more CPU cycles than the IF statements.

...R

Thanks all. This has helped a lot. The case I had was an event that if a button is pushed (virtual pin) then check if the period of time since the last event has been exceeded, then check something else.

I realised after my first post that I was going about it wrongly and it wasn't clear what I wanted, but I was still curious about how the if statement would work, so gave the example in a simpler way to see if I had the concepts right.

The other methods you've both suggested has gone in the Arduino database in my brain (I'm still somewhat a novice but getting there) so this has been helpful. I have many more plans for arduino and esp8266 projects.

I found I could reduce the complexity of the code of my real application, by simply having a single if and else statement.

If the button is high, then put all my variables to default settings and start a simple timer (countdown of 1 day) that runs a function to turn the button on/true when it lapsed. This statement gets called every 5 minutes so starts the timer again before the time period has lapsed while the button is high/true.

Else if the button is false/low then do some stuff, and because the simpletimer timer hasn't been called once the button goes false/low, then after 24 hours the button will go high/on and stop this code running.

So it's a timed latched button/switch. Haven't tested it yet, but it seems like a better way to go about it.. Now I think about it, perhaps I should search for timed latched buttons or switches as it wasn't clear exactly what I needed until I rambled on here