I declared and initialized a global variable, before setup(), like this:
int startVal = -1;
In setup() I wrote the value to an OLED display and it was garbage: -16593
Then I added a single line to setup():
Serial.begin(9600);
And then the value displayed on the OLED was what I wanted: -1
I can initialize the variable within setup() instead of as part of the declaration. But why would including the Serial class affect the initialization of this variable? Are there other circumstances where initializing in this manner will fail?
There was/is a bug in some bootloaders where -1 was not being uploaded to program memory (being 0xFF which is the default value after clearing memory). In particular this would happen if it was the last variable. Adding a Serial.begin would have added more variables, so your -1 might not have been last any more.
Thank you. I had just noticed that using a different value works. I was puzzling over that when I saw your post. I'm glad you were there and remembered so I wouldn't have to go through it again. That was long before before I had even heard the name "Arduino".
So it's a bug in the bootloader.
That thread is nearly three years old and I bought my Uno less than 3 months ago. So I guess there's no rush to fix it. At least it only shows up if the last variable being initialized is set to -1. I'll have to try and remember that.
I seem to recall in that thread was a change I suggested that fixed it. It fitted into the available bootloader program memory too. Ah well, sometimes the wheels turn slowly.