Using "&&" with multiple terms

void levelread(){
if (level_5 == 0){
  level_factor = 0;
}

if (level_5 == 1){
  level_factor = 1;
}

  
if (level_4 == 1 && level_5 == 1 ){
  level_factor = 2;
}


if ( level_3 == 1 && level_4 == 1 && level_5 == 1){
  level_factor = 3;
}

if ( level_2 == 1 && level_3 == 1 && level_4 == 1 && level_5 == 1){
  level_factor = 4;
}

if ( level_1 == 1 && level_2 == 1 && level_3 == 1 && level_4 == 1 && level_5 == 1){
  level_factor = 5;
} 

}

i need to check if all variables are HIGH (1) in this "if" functions

weirdly it checkes only the first and last in every "if", meaning in this "if

if ( level_3 == 1 && level_4 == 1 && level_5 == 1){
  level_factor = 3;
}

it writes "level_factor = 3" even if only "level_3" and "level_5" are HIGH but "level_4" is LOW

am i using it right?

thank you

There is no default value. If it was level_factor 3 and level_4 drops low, it will still read level_factor 3 as there is nothing to change it otherwise. What do you want the output to be if level_3=HIGH, level_4=LOW, and level_5=HIGH?

Your example (level_3=HIGH, level_4=LOW, and level_5=HIGH) shows there are more possibilities than what you are checking (1=HIGH, 1=2=HIGH, 1=2=3=HIGH, 1=2=3=4=HIGH, 1=2=3=4=5=HIGH, 5==LOW and 5==HIGH). What are all the possibly combinations and what value should be output for each?

This assumes that level_x is a boolean data type, since only a snippet of the program was posted without using code tags.

weirdly it checkes only the first and last in every "if"

It should be simple to post a complete program proving that.

Over to you

Yamsoussana:

if ( level_3 == 1 && level_4 == 1 && level_5 == 1){

level_factor = 3;
}



it writes "level_factor = 3" even if only "level_3" and "level_5" are HIGH but "level_4" is LOW

am i using it right? 

thank you

I am sure you're mistaken, such a compiler bug would have been spotted long ago and by automated tests.
Post your entire code - there are many possible reasons for anomalous behaviour, running out of RAM is
a common one.

BTW your code can be shortened by using an auxiliary function like so:

int level_factor()
{
  if (level_5 != 1)
    return 0;
  if (level_4 != 1)
    return 1;
  if (level_3 != 1)
    return 2;
  if (level_2 != 1)
    return 3;
  if (level_1 != 1)
    return 4;
  return 5;
}

And could be even shorter and simpler if all those level_N variables were an array.

MarkT:
I am sure you're mistaken, such a compiler bug would have been spotted long ago and by automated tests.
Post your entire code - there are many possible reasons for anomalous behavior, running out of RAM is
a common one.

Also note that if it was set to a matching level (level_factor = 3), then in a situation where none of the conditions are met (such as the HIGH-LOW-HIGH in the example), it will retain the last value. There is no "default" state for the case where none of the conditions are met.

We need OP feed back on all questions.

Sorry, I forgot to mention all the pins (level_X) are INPUT_PULLUP.

the code is a part of a big project, its a bit irrelevant to post it if this way of writing the "&&" is incorrect.
if it is correct, i will go and check other parts of my code for errors.

Thank you very much for the help.

(deleted)

"the code is a part of a big project, its a bit irrelevant to post it if this way of writing the "&&" is incorrect."

Isn’t it your responsibility to reproduce the condition in a smaller sketch?

Yamsoussana:
Sorry, I forgot to mention all the pins (level_X) are INPUT_PULLUP.

the code is a part of a big project, its a bit irrelevant to post it if this way of writing the "&&" is incorrect.
if it is correct, i will go and check other parts of my code for errors.

Thank you very much for the help.

From what I see in what you posted, it is not incorrect. It is incomplete. There are conditions possible (see post #2) that you have not accounted for.

Make a little truth table
Level
1 2 3 4 5 Output
0 0 0 0 0 0
0 0 0 0 1 1
0 0 1 0 1 ?

You need to account for all 32 possible conditions. The program has 6th and you mention a 7th, what about the other 25?

With that said, why not turn the 5 bits into an integer, then use a case statement. Guaranteed to catch all 32 possibilities.

its a bit irrelevant to post it if this way of writing the “&&” is incorrect.

The code snippet that you posted looks OK. What we are suggesting is that you post a smaller but complete program that demonstrates the problem.

If you are convinced that some of the conditions are being skipped then print their values before testing them.

Hey,

Many thanks to everyone who addressed the specific issue I asked about.
Your reassurance that this code is viable gave me the confidence to move on to other parts of the software/hardware in order to find the problem.
bottom line - I've found it, and that way of writing sure works.

For future searches - it is recommended on other sites to write every comparison in its own brackets like that -

if  (( level_5 == 0) && (level_4 == 0) && (level_3 == 0) && (level_2 == 0) && (level_1 == 0)){
  level_factor = 0;
}

Once again...
Thank you all

Yam

You original if statement could be boiled down to:

level_factor = (level_3 + level_4 + level_5 == 3) ? 3 : 0;