Problem Setting Booleans

I am having issues setting bool variables. It seems they randomly don't set correctly. If I change the variable name to something completely different, they start to work. If I change it back to the name that wasn't working, they stop working again. I have checked that I am not using any reserved names.

For example the below code prints that flashFlood is false:

bool flashFlood = false;

void setup() {

     Serial.begin();

}

void loop() {

    if (1 <= 2) { 
      flashFlood = true;      
    }

    else {
      flashFlood = false;
    }

   Serial.println(flashFlood);

}

but the below code works, and prints that flashFloodd is true....

bool flashFloodd = false;

void setup() {

     Serial.begin();

}

void loop() {

    if (1 <= 2) { 
      flashFloodd = true;      
    }

    else {
      flashFloodd = false;
    }

   Serial.println(flashFloodd);

}

Any thoughts? I have a similar problem with other variable names. It seems that there is about a 50% chance a certain name won't work with booleans? I am using an Arduino MKR NB1500 in the Arduino IDE. I have tried on 2 different MKRs with the same result.

Your code does not seem to compile

C:\Users\sterretje\Documents\Arduino\rc9\test_changeBoard\test_changeBoard.ino:5:16: error: no matching function for call to 'Serial_::begin()'
   Serial.begin();
                ^

The optimizer can solve the if at compile time and will get rid of everything and the loop will just be

Serial.println(1);

Serial needs to be setup the right way to see anything - and it’s going to fill up the outgoing buffer very quickly

If you submit technically valid data to the Computer, then it can do something meaningful for you. Is 1<= 2 statement technically correct?

What does your question mean?

To ask, as @twist27896 did, if (1 <= 2) is "technically correct" (whatever that may mean), although the answer is of course always going to be false oops true. They were obviously using that as a way to force the program flow into one specific half of the if structure, in much the same was as one might force a stop in a program with a while(1) which is always true.

My intention was to know what OP was answering -- yes or no as by measurement 1 is not equal to 2!

Yes, it's testing too many features at one time. A test of the claimed (dubious) phenomenon is just:

void loop() {
      flashFlood = true;      
   Serial.println(flashFlood);
}

...unless you are testing the 'if' statement. Adding that makes it no longer an MRE.

The term (1 <= 2) returns boolean true because 1 is less than or equal to 2 as described here https://www.arduino.cc/reference/tr/language/structure/comparison-operators/lessthanorequalto/

For this reason both sketches from post #1 should give the same result.

I second @J-M-L 's post regarding the missing baud rate and would expect that both sketches should not compile and produce this error:

exit status 1
no matching function for call to 'HardwareSerial::begin()'

Not for me. After adding the required baud rate value I run the sketch on an Arduino UNO and it reports '1' (== true) over and over.

1 Like

What I suspected. :slight_smile: The posted code is not the test code.

Exactly, compiling the sketches should produce the error from post #8 ...

Ah, I forgot to add the baud rate in the post. I set it to 9600 in the actual code. Still getting the error.

Hello twist27896

Take this simple example and gain the knowledge about.

bool flashFlood = false;
void setup() 
{
  Serial.begin(9600);
  Serial.println("");
  Serial.println("1 <= 2");
  if (1 <= 2) 
  {
    flashFlood = true;
  }
  else 
  {
    flashFlood = false;
  }
  Serial.println(flashFlood ? "true" : "false");
  Serial.println(flashFlood);
  Serial.println(1 <= 2);
  
  Serial.println("");
  Serial.println("2 <= 1");
  if (2 <= 1) 
  {
    flashFlood = true;
  }
  else 
  {
    flashFlood = false;
  }
  Serial.println(flashFlood ? "true" : "false");
  Serial.println(flashFlood);
  Serial.println(2 <= 1);
}
void loop(){}

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.

Post the exact code that gives the error. Use the ide copy for forum and paste it here.

Or post #2 :slight_smile:

1 Like

I tried compiling and uploading on a different laptop and it fixed the issue. I reinstalled the Arduino IDE and it works on the original laptop now as well. This fix doesn't really make sense but I guess it works...

you sure?

Are you implying that c++ compiler has somehow compiled this simple sketch with errors on one laptop?

Uggggg... brain failure. Thanks.

I guess, I don't understand why this fixed the issue.