I have successfully used my BMP280 baro sensor many times. But there is part of the default code from examples that always confused me, it is:
if (!bmp280object.init()) {Serial.println("Device error!");}
I realize that it is checking to see if the bmp280object is initialized, and if not, prints an error message.
My question is this: In the code the bmp280object is NEVER formally initialized with a direct init() command, but it DOES get initialized and I never get an error.....
Does simply referencing the init() command in the if statement automatically initialize the bmp280object?
This is what I infer from the behavior, and its maybe really a CPP question, but any info to broaden my understanding would be appreciated. Thanks!
Perfect, just what I needed to know. I find it interesting (and also not transparent exactly) that in the if statement when the init() method is called to check its status, the if statement seems to automatically know that it has a good initialization (ie true or false). Its like it knows to expect a boolean?
Also, in your example, I admit I am further confused (my beginner status with CPP) by this statement:
int bmpError = bmp280obkectInit();
I see that you set up bmpError as an int, but then assign it to bmp280obkectInit();, but what is bmp280obkectInit(); exactly? (its not the initialization statement/method itself which would read bmp280object.init()
The expression in the parentheses is evaluated, sometimes that will have side effects, like in this case a function is called and executed.
The expression will end up being true or false; in this context zero is false, and anything not equal to zero is true.
About the only other thing to know right here is that it is folly to expect a floating point number to be 0.000000000000000, so you can do something like
if (abs(realTemperature) < 0.001) { // temperature is basically zero
take the absolute value of an expression and see if it is close enough to zero. Some ppl use a global manifest constant like
ok thanks, excellent comments. I get your point that actually executing the init() method within the if statement is kind of by product, but also, useful.