Hello all
I am trying to use an array of bool.
The idea would be to declare it and initialize it on the go.
So far I only get it running if I declare and initialize it at the same time:
This works (declaring / initializing):
bool nextStep[3] = {false, false, false};
void setup()
{
Serial.begin(115200);
while (!Serial);
nextStep[0] = true;
}
void loop()
{
Serial.print("NextStep 0: ");
Serial.println(nextStep[0]);
}
This doesnt work (just declaring):
bool nextStep[3];
void setup()
{
Serial.begin(115200);
while (!Serial);
nextStep[0] = true;
}
void loop()
{
Serial.print("NextStep 0: ");
Serial.println(nextStep[0]);
}
If I replace the data type with byte or int the second example works just fine.
What is the difference? How to make the second version of array of bool work?
Thank you...
gcjr
March 22, 2020, 9:48am
2
compiles for me
bool nextStep[3];
void setup()
{
Serial.begin(115200);
while (!Serial);
nextStep[0] = true;
}
void loop()
{
Serial.print("NextStep 0: ");
Serial.println(nextStep[0]);
}
There is nothing wrong with your second code, what error do you get when you try to compile it? What does 'doesn't work' mean?
This doesnt work (just declaring):
What does "doesn't work" mean?
In your second example, all the elements of next step are set to false before setup is called.
TheMemberFormerlyKnownAsAWOL:
What does "doesn't work" mean?
In your second example, all the elements of next step are set to false before setup is called.
Thank you for the replies.
@TheMemberFormerlyKnownAsAWOL :
I tried to assign true to the first element in setup:
nextStep[0] = true;
But when I print it the value is still false.
By doens't work I don't mean that it doesn't compile, but I only seem to be able to assign values if I have initialized the array after declaring it.
system
March 22, 2020, 10:27am
6
moserroger:
But when I print it the value is still false.
Not for me.....
NextStep 0: 1
NextStep 0: 1
NextStep 0: 1
Weird.
Yes you are right. Now it also works for me.
Don't know what happened before.
Sorry for the an unnecessary question...!
Sorry for the an unnecessary question...!
Did you learn something? If you did then this was worthwhile.
moserroger:
Weird.
Yes you are right. Now it also works for me.
Don't know what happened before.
Sorry for the an unnecessary question...!
Probably either you clicked on "verify" instead of "upload", or the upload failed (I seem to be getting more failed uploads in recent versions of the IDE, not sure if its something that changed in the IDE or in the operating system on my computer).