I compiled the example below for arduino nano and found that the variable assignment outside the setup doesn't work as intended.
Following the tutorial at https://www.arduino.cc/en/Tutorial/Variables I though I was perfectly fine.
Playing around with different values though showed me that it only works properly when I assign the values within setup().
Is this a known problem or am I doing something wrong here?
UKHeliBob:
Note that a boolean variable should only have a value of true or false. Other values may work but your mileage may vary.
You can do this:
bool myBool = 7;
and myBool will have a value of true. Any non-zero value assigned to a bool variable is converted to true. This is guaranteed by the C and C++ standards.
See n1570 (final draft of C11; C++ standards have similar language):
6.3.1.2 Boolean type
1 When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.
And what are their value if you don't assign than in setup()?
Everyways if you want like this means that yiu want const variables (that can conteins, for example, pins or Serialbcomands, or delays). If so you can use #define. It simply change the first word wirch all the following things, so the program recive a number directly
//how to use #define
#define variablename value
//NO =, NO ;
When you use an instruction, look it up , or look at the examples in the IDE. You can’t “not care “‘about syntax and usage and then wonder why something doesn’t work . The use of standardisation also means others can understand your efforts and offer advice.
PaulS:
No. It will have a value of 7. That value will be interpreted as true, because it is non-zero, if used as a boolean.
if(myBool)
{
// This is true
}
if(myBool == 7)
{
// Hey, this is true, too
}
Nope, a bool cannot hold any value other than 0 or 1 (false or true). Read the link to the C11 standard I posted above.
Edit to add: In your example above, the value of myBool will be converted to int before it's compared with 7 (because int is "wider" than bool). Then it's a comparison of 1 == 7, which is false.
I apologize for the confusion.
The assignment of values at declaration time is perfectly fine.
The problem was a loose contact on my breadboard which made the LED output behavior spurious and led me to the the wrong conclusion.
boolrules:
Yup. The compiler assigns 1 to a bool for any non-zero value on the right hand side of an expression.
Also in hardware/tools/avr/lib/gcc/avr/4.9.2/plugin/include/system.h:
# define bool unsigned char
What version of the Arduino IDE is that found in? That's not the standard definition of "bool". I have 1.8.5 and there is no file named "system.h". In hardware/tools/avr/lib/gcc/avr/4.9.2/include/stdbool.h you'll find this line:
#define bool _Bool
That's for C. For C++ "bool" is already defined by the language without requiring stdbool.h. (_Bool is defined by the language in C99 and later; bool is a synonym when stdbool.h is included.)
Hmm, it's not in a fresh 1.8.3 either (I downloaded the standalone version so I don't have to install it). There's not even a "plugin" directory. Is this file from a core like the ESP2866 perhaps?
After a little investigation, that system.h file seems to be used by GCC to build itself. It's not used by applications that are built by GCC (such as Arduino sketches).
Here's a comment in the code preceding all of the Boolean macro definitions in system.h:
/* Provide a fake boolean type. We make no attempt to use the
C99 _Bool, as it may not be available in the bootstrap compiler,
and even if it is, it is liable to be buggy.
This must be after all inclusion of system headers, as some of
them will mess us up. */