Compare two boolean in Arduino

In my program, I need my Arduino to run a function when one boolean is not equal to another, I try to use if(streaming_switch!=in), but I get an error report:



sketch\PCIO.h:186:26: note: candidate: operator!=(int, int) <built-in>

sketch\PCIO.h:186:26: note: candidate: operator!=(int, unsigned int) <built-in>

sketch\PCIO.h:186:26: note: candidate: operator!=(int, long int) <built-in>

sketch\PCIO.h:186:26: note: candidate: operator!=(int, long unsigned int) <built-in>

sketch\PCIO.h:186:26: note: candidate: operator!=(int, float) <built-in>

sketch\PCIO.h:186:26: note: candidate: operator!=(int, double) <built-in>

It seems that != operator cannot be used to compare boolean variables in Arduino.
Is there any alternative method to get the "not equal" condition between two boolean values?

Don't post snippets (Snippets R Us!)

can we really see the type of streaming_switch and in

this compiles and works fine

bool in, streaming_switch;

void setup() {
  in = true;

void loop() {
  streaming_switch = (random(0, 10) > 5);
  if (in != streaming_switch)

Thank you very much. "in"is a variable reading from a library and its definition is not clear in my program. It works after I replace it by if(streaming_switch!= (bool)in).

I'm sure that we can find the type if you share the library information

casting to bool might not be what you want

Thank you for your help. It is now working as I expect.
The library is called ThingerWiFiNINA.h, which is used to connect the device to a commercial server. From the introduction on their website, "in" should be a boolean value. But it may cast to double as all the variables are casting to double before transmission. Therefore, it should be fine if I just cast it back to bool.

glad it works but it still does not make sense:
a boolean converted to a double will still be worth 0 or 1, so when you do the comparison with a boolean, the boolean is promoted to 0 or 1 which will match the double value (0 and 1 are precise in double format)
may be it's not a bool but an enum and 0 is 'false' and you have multiple other values possible that are non 0
in that case comparing true and say 7 won't indeed work unless you cast 7 back to a bool (which the spec says will be true if non null)

I can't find the definition of "in" from the library, but they also have similar commands like:
in = (bool) digitalRead(pin);
Casting a number to boolean will get true if the number is non-zero and false if the number is zero. They might just send 1 to my arduino if I select true on the server.


it's not great coding as they rely on HIGH and LOW to be 1 and 0

in = (digitalRead(pin) == HIGH) ; 

would be better

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.