Strategy Question - Four conditions must be met for One event to happen

I'm a noob and I'm looking for help in a programming strategy.

I have four conditions that all must be met in order for one event to happen. Comparison loops can only test between two conditions at most. I currently have four IF statements but three could be false and the fourth would continue to loop. What I was hoping for is something like an If statement that is true if Cond1, Cond2, Cond3 and Cond4 are all true.

Do you have an idea?

ShowerMaster

I have no idea what a "comparison loop" is, but I suggest you look at the "&&" operator.

Hi, Try this sketch, it only uses 3 IF statements.

if (digitalRead(1) == HIGH  && digitalRead(2) == HIGH) // check inputs 1 and 2
{ 
     INPUT12 = true               // if both high then high result
}
else
{ 
     INPUT12 = false               // if either or both low then low result
}


if (digitalRead(3) == HIGH  && digitalRead(4) == HIGH) //check inputs 3 and 4
{ 
     INPUT34 = true              // if both high then high result
}
else
{
     INPUT34 = false               // if either or both low then low result
}

if (INPUT12 == HIGH && INPUT34 == HIGH) // check sub inputs 12 and 34
{ 
     MAINOUTPUT = true           // if all 4 inputs high then high result
}
else
{
     MAINOUTPUT = false           // if any or all inputs low then low result
}

digitalWrite (5,MAINOUTPUT);

Remember to keep adding // comments as you go, its good habit and practice to get into, it helps us to follow your sketches and you to debug as your sketch gets larger.

Hope this helps...

Tom.... :slight_smile:

Ps, sorry this sketch was done as a quick example, you will have to declare INPUT12, INPUT34 and MAINOUTPUT as boolean.

Generally there should be no reasonable limit on how may AND conditions you can test for.
if( A AND B AND C AND D AND E,,,)

Also
if condition test fails execution continues with false process. There is no need for "else" - in this case anyway

In essence
if ( condition )
true....
false...

Vaclav

TomGeorge:
Remember to keep adding // comments as you go, its good habit and practice to get into, it helps us to follow your sketches and you to debug as your sketch gets larger.

It is, however, pointless when your comments merely restate what the code does. Use comments to explain how or why the code is doing something, when this isn't obvious.

I couldn't figure out why you used multiple if statements instead of just calling digitalWrite() with the appropriate logical expression:

digitalWrite (5, (digitalRead(1) == HIGH)  && (digitalRead(2) == HIGH) && (digitalRead(3) == HIGH) && (digitalRead(4) == HIGH));

Vaclav:
if condition test fails execution continues with false process. There is no need for "else" - in this case anyway

In essence
if ( condition )
true....
false...

Depending what you're trying to say that is either misleading, or wrong.

Hi, I did good, look how many different solutions appear when you suggest a long one.
There you are ShowerMaster, take your pick.

Tom... :slight_smile:

Just to muddy the water further... you can use a loop to scale to lots and lots of conditions without your if statement becoming insanely long:

byte conditionCounter = 0;
for(byte i = 2; i < 6; i++)
  {
  if(digitalRead(i))
    { conditionCounter++; }
  }
if(conditionCounter>3)
  { digitalWrite(outputPin, HIGH); }

PeterH,
sorry I got sidetracked and did not return here to correct that , you are correct.
The If / else writeup was both - incorrect and misleading.
My apology to all.
Vaclav

got it .. thanks ..