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.
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.
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.
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.
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 -