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:
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?
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).
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.