I had learned to put up with and ignore the double-spew of initial data written to the serial monitor when installing new sketches. In case anyone hasn't seen it, the first several lines written to serial monitor always seem to get repeated.
But today, while trying to figure something else out, I added a line to light an led just to see if it was reached.
And the LED blinked twice.
After some tinkering, I stripped everything out of the program, and even switch boards.
And then added another LED and a delay.
The circuit is trivial; there are LEDs attached to pins 6 and 7 thorough 330ohm resistors to ground. (there are still a pair of buttons on the breadboard, but they're not connected).
I distilled code to
void setup() {
//get the monitor going
Serial.begin(9600);
Serial.println("starting setup");
//set input LED modes
pinMode(6, HIGH);
digitalWrite(6, HIGH);
delay(250);
digitalWrite(6, LOW);
pinMode(7, HIGH);
digitalWrite(7, HIGH);
delay(250);
digitalWrite(7, LOW);
Serial.println("***leaving setup***!");
} //end of setupairp.a
void loop() {}
The video starts as I hit compile. We see
- the green LED lights (after compile, at the beginning of the orange output in the IDE),
- the flickering of the board status LEDs a the sketch is installed,
- the two LEDs lit in sequence
- about a second later, they light again.
The sketch still in at the beginning had the red LED code commented out. Comment it out again, install again, and both will come on at compile, and then the green will blink twice. Etc.
What is going on?
It kind of makes sense that a reset is sent by USB to start programming, and at the end of programming to send it on its way, but that only accounts for two (and even so, letting it run a couple of seconds is odd!)
Also note that the initial "starting setup" prints twice (about a second apart). setup() is apparently stopped before reaching "leaving setup" (or, before it leaves the buffer).
This is consistent with past observations that only the initial output is double printed.
Until today, I'd assumed that this was simply a quirk of the IDE, rather than that the mcu was executing twice.
video: http://dochawk.org/ard/IMG_1638.m4v (I can't make it display here other than a high-flicker gif!)