1

I am trying to use boolean variables with two if statements. The second if condition never evaluates to be true. May be it's a problem with how I have defined my boolean variables. Do you see the mistake?

Here is the code:

int x = 0;
boolean state1 = (x <= 10);
boolean state2 = (x > 10);

void setup() {
  Serial.begin(9600);
}

void loop() {

  x++;

  if (state1) {
    Serial.println("x <= 10");
  } else if (state2) {
    Serial.println(" x > 10");
  }

}
3
  • You never update your state variables. When you assign in C you assign the result of the formula, not the formula itself. – Majenko Mar 27 '20 at 13:37
  • I was trying to assign the two conditions to the boolean variables so yes like you said I was assigning the formula. – Zaffresky Mar 27 '20 at 14:26
  • This question does not concern the Arduino but is a general programming question. – StarCat Mar 27 '20 at 17:10
1

I asked this question to be moved to Stackoverflow as it is not Arduino related.

However to answer your question, you assign the booleans before you start the program, so they are never re-evaluated and always keep their initial value, based on when x = 0.

What you should do is, reevaluate them within the loop:

int x = 0;
boolean state1;
boolean state2;

void setup() {
  Serial.begin(9600);
}

void loop() {
  x++;
  state1 = (x <= 10);
  state2 = (x > 10);
  if (state1) {
    Serial.println("x <= 10");
  } else if (state2) {
    Serial.println(" x > 10");
  }
}

Also, because if state1 is true, state2 will always be false and vice versa, so you don't need to have two booleans. This will give:

int x = 0;
boolean state;

void setup() {
  Serial.begin(9600);
}

void loop() {
  x++;
  state = (x <= 10);
  if (state) {
    Serial.println("x <= 10");
  } else {
    Serial.println(" x > 10");
  }
}

Beyond that, you don't need to define the boolean globally, because you only need it inside the loop and is not need to be 'stored' from one loop to another. So you get

int x = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  x++;
  boolean state = (x <= 10);
  if (state) {
    Serial.println("x <= 10");
  } else {
    Serial.println(" x > 10");
  }
}

Of course you can also directly check for x <= 10 instead of storing it in a boolean, but I assume you need the boolean elsewhere further within the loop function later.

4
  • 2
    Yes I see the issue now. I saw a few sketches where boolean variables were used in if statement and thought that I could use them to store the conditions itself. I was assuming that since the condition is now stored / assigned to the boolean variable so every time the variable is used it will automatically compare the condition to the current value of x. But your code helped me see the need to update the variables and storing them locally. I understand it better now. Thanks – Zaffresky Mar 27 '20 at 14:41
  • You are welcome ... True, there is a difference between declaring the variables (boolean b), and assigning (b = x). When x changes, b does not change, until you perform a new assignment (unless it is a pointer). I suggest to read about C/C++, it makes using the Arduino much more powerful). – Michel Keijzers Mar 27 '20 at 15:14
  • 1
    Yes I do plan to learn more C/C++ but at this stage just getting familiar with basic electronics and writing simple arduino sketches. So the next step is definitely digging deeper into C. – Zaffresky Mar 27 '20 at 15:39
  • You cannot do anything at the same time (I'm also still learning electronics) – Michel Keijzers Mar 27 '20 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.