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.
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....
Ps, sorry this sketch was done as a quick example, you will have to declare INPUT12, INPUT34 and MAINOUTPUT as boolean.
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:
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