Nested boolean operators

Is there a way I can "nest" boolean operators?

I have 10 sensors that are all being read individually, but their read values will all be connected with ||

I currently have a test that looks like

if (sensorState1 == LOW || sensorState2 == LOW || sensorState3 == LOW) {     
    digitalWrite(LEDPIN, HIGH);  
  }

but will be incorporating this into another aspect where it would be something like this

if (phoneTrip == 1 && (sensorState1 == HIGH || sensorState2 == HIGH etc.)){ //if the sensor is tripped
        analogWrite(REDPIN,255);
        analogWrite(GREENPIN,255);
        analogWrite(BLUEPIN,255);
        state = OPENING_1;
        StartOpenTime = millis();
      }

You can string together as many as you want and use parentheses as necessary.

Can you put the values to be tested in an array as you read them, perhaps using a for loop ?

If so then you could read all of the sensors then iterate through the array and sum the values. If the sum is not zero then one or more of the values was at least 1 (HIGH).

Alternatively, can you read the sensors in a for loop and exit as soon as you find one that is HIGH ?

When you find yourself tempted to suffix variable names with numbers

if (sensorState1 == LOW || sensorState2 == LOW || sensorState3 == LOW) {

it is time to learn about arrays.

An array and a for loop would make this much easier.

vaj4088:
When you find yourself tempted to suffix variable names with numbers

if (sensorState1 == LOW || sensorState2 == LOW || sensorState3 == LOW) {

it is time to learn about arrays.

An array and a for loop would make this much easier.

Say what? You cannot be serious.

The parentheses are for human readability, the compound condition ( that's the name of it ) is evaluated from left to right.
The evaluation is stopped when the condition is evaluated as false - no need to continue the evaluation.
It's the real beauty of Boolean algebra.
Jim

julyjim:
The evaluation is stopped when the condition is evaluated as false - no need to continue the evaluation.
It's the real beauty of Boolean algebra.
Jim

The evaluation is stopped (called short circuiting) when the condition can be determined one way or the other. For && it is false, but for || it is true.

julyjim wrote (in part):

if (sensorState1 == LOW || sensorState2 == LOW || sensorState3 == LOW) {

The parentheses are for human readability

The parentheses are required by C/C++ syntax for the "if" statement.

julyjim wrote (in part):

The evaluation is stopped when the condition is evaluated as false - no need to continue the evaluation.

A for loop can do this too (breaking on true when evaluating || ). The for loop might be slightly slower (I have not benchmarked it) but is much easier and less error prone when there are ten sensors involved.

I'm familiar with for-loops, but I don't understand exactly how the code should be written. like how can I append the counter 'i' to the string sensorState and then add those values?
??
for (int i=1; i <11; i++){
+= "sensorState + i"
}
??

You cannot "...append the counter 'i' to the string sensorState and then add those values".

You need to learn about arrays. If you learn best by reading, then the tutorial at
http://www.cplusplus.com/doc/tutorial/arrays/
may be helpful. If you need a video, Bing and Google are your friends. Useful search terms are "C++ array tutorial".

Furthermore, the loop could do addition but I would not choose to do it that way.

vaj4088:
julyjim wrote (in part):

if (sensorState1 == LOW || sensorState2 == LOW || sensorState3 == LOW) {

read post #1 and follow the discussion from there

The parentheses are required by C/C++ syntax for the "if" statement.

julyjim wrote (in part):

A for loop can do this too (breaking on true when evaluating || ). The for loop might be slightly slower (I have not benchmarked it) but is much easier and less error prone when there are ten sensors involved.

vaj4088:
julyjim wrote (in part):

if (sensorState1 == LOW || sensorState2 == LOW || sensorState3 == LOW) {

The parentheses are required by C/C++ syntax for the "if" statement.

julyjim wrote (in part):

A for loop can do this too (breaking on true when evaluating || ). The for loop might be slightly slower (I have not benchmarked it) but is much easier and less error prone when there are ten sensors involved.

julyjim:
The parentheses are for human readability, the compound condition ( that's the name of it ) is evaluated from left to right.

That's not the case. && has higher precedence than ||. It's a real trap for young players. Old ones too, sometimes. Use parenthesis.

PaulMurrayCbr:
That's not the case. && has higher precedence than ||. It's a real trap for young players. Old ones too, sometimes. Use parenthesis.

PaulMurrayCbr:
That's not the case. && has higher precedence than ||. It's a real trap for young players. Old ones too, sometimes. Use parenthesis.

Well, of course you are correct, but it was initially case of all ORs so I tried KISS.
Besides the OP is being pushed to use array ,and I can't wait how it will pan out.
Maybe I'll learn something useful.
Jim

julyjim:
Well, of course you are correct, but it was initially case of all ORs so I tried KISS.

Problem is that what the OP wants to do is this:

if (phoneTrip == 1 && (sensorState1 == HIGH || sensorState2 == HIGH etc.))

Which absolutely will to the wrong thing without the grouping.

Besides the OP is being pushed to use array ,and I can't wait how it will pan out.

Yeah, I know. But with 20 sensors, it's really the only sensible way to go. Hey - if it was me dishing out advice, I'd be advising the OP use classes. Depends on how much behaviour the sensors have, of course.

Can we please have more details of the sensors in order to provide further advice. If they are all digital sensors, which seems likely, then put their pin numbers in an array and iterate through the array to read each pin using digitalRead()

Like people said, you need to use arrays. Something like:

byte mypins[] = {1, 3, 5, 7, 9};

boolean pinsAllHigh()
{
    for (byte i=0; i<sizeof(mypins)/sizeof(*mypins); i++) {
        if (digitalRead(mypins[i]) == LOW) {
            return False;
        }
    }
    return True;
}

boolean pinsAllLow()
{
    for (byte i=0; i<sizeof(mypins)/sizeof(*mypins); i++) {
        if (digitalRead(mypins[i]) == HIGH) {
            return False;
        }
    }
    return True;
}

boolean pinsAnyHigh()
{
    for (byte i=0; i<sizeof(mypins)/sizeof(*mypins); i++) {
        if (digitalRead(mypins[i]) == HIGH) {
            return True;
        }
    }
    return False;
}

(Now envisioning an awful hack where you send in a list of pins as a hex sting:

 if (pinsAllHigh("12CD")) {  //check pins 1, 2, 12, and 13...

In all the above cases, the for loops are going to be pretty light-weight compared to digitalRead() itself, and comparable to whatever a compound boolean statement would do.

Rather than arrays, I'd be inclined to use individual bits in a suitably-sized variable.

(sp. "true", "false")

(sp. "true", "false")

rats. I should have checked. (too much Python, recently.)

Did you know that "b &&= myBooleanFunc();" isn't legal? Hmmph.

westfw:
pins as a hex sting:

#include <voodoo/curses.h>

oqibidipo:
#include <voodoo/curses.h>

:smiley:

PaulMurrayCbr:
Problem is that what the OP wants to do is this:

if (phoneTrip == 1 && (sensorState1 == HIGH || sensorState2 == HIGH etc.))

..and that is what I like to see done in an array...

Which absolutely will to the wrong thing without the grouping.
Yeah, I know. But with 20 sensors, it's really the only sensible way to go. Hey - if it was me dishing out advice, I'd be advising the OP use classes. Depends on how much behaviour the sensors have, of course.