Code runs twice on 1.8.13?

I've run into a problem with my Mega2560 where code seems to be running twice. Almost like the 2560 is resetting and running the code again from the start, But this only "resets" once. Problem is only on my Mega2560. Tested on Uno and no issues there.

No matter how complicated the code it always starts and then re-starts again from the setup.

Arduino IDE 1.8.13 on Ubuntu 20.04
I've retested on 1.8.12 and the issue is not there. Is this some bug with .13?

Code and example will explain the issue:

void setup() {

  Serial.begin(115200);
  Serial.println("Setup");
}


void loop() {
  Serial.println("Main");
  while(1);
}

Output:

Setup
Main
Setup
Main

Open serial monitor, upload code and output is shown.

When you open Serial Monitor, it resets your Mega board. I think what happens is the original Serial input manages to get printed in Serial Monitor, then it resets and the program runs again. You could test it by pressing the reset on the board while the Serial Monitor is open. You should only see one additional set of Setup/Main printed to Serial Monitor.

The only thing related to Serial Monitor I see mentioned in the 1.8.13 release notes is:

  • Allow baudrate/config change when the Serial Monitor/Plotter is disabled.

It doesn't seem very relevant though.

The change that came to my mind ("Serial monitor is no more cleared during upload, so it can be copy/pasted") turned out to have been made in 1.8.12, so that certainly isn't the cause of the different behavior you're seeing.

Thanks for the explanation. I've tried pressing the reset button, and you are right. The code only runs once.

I've done some more testing and on .13 if I upload the code, then open serial monitor it runs as expected.
If however I leave the monitor open, re-upload the code then it runs, resets and runs again. (The issue on the OP is with the serial monitor kept open)

This is different from .12 (also tested on .11)

Not really a big issue now that I know serial monitor is resetting the device.

You're welcome.

Because a port can only be connected to one application at a time, in order for the upload to work while Serial Monitor is open, the Arduino IDE has to do a dance of closing and opening the port in the Serial Monitor. It's the opening of the port that resets the Mega (change of state of the DTR pin). So the process goes like this:

  • Upload starts
  • Serial Monitor closes the port
  • Upload tool opens the port, resetting the Mega
  • Upload finishes
  • Serial Monitor reopens the port, resetting the Mega

It's the opening of the port that resets the Mega. So even though you aren't manually opening the Serial Monitor application (because it was always open), it still automatically opens the port and causes the reset.

I wonder if that "Allow baudrate/config change when the Serial Monitor/Plotter is disabled." change is causing the procedure to now work like this

  • Upload starts
  • Serial Monitor closes the port
  • Upload tool opens the port, resetting the Mega
  • Upload finishes
  • Serial Monitor reopens the port, resetting the Mega
  • Serial Monitor reconfigures the port, resetting the Mega

One thing I've noticed lately is that if I run a sketch with 115200 baud serial output, but I have the Serial Monitor set to some other baud rate. That initial serial output still prints correctly, even though it shouldn't due to the baudrate mismatch. This doesn't happen with any other baud rate. That makes me think the initial port opening always happens at 115200, then it's reconfigured to whatever the actual settings are in the Serial Monitor.

@Per

Baudrate mismatch on many of the newer boards is not an issue due to the way they handle comms.

Take an MKR board and set the serial monitor to one baud rate and the board to another and it SHOULD auto adjust to give a useful output.

Only tested with earlier IDE's and not 1.8.13 and also works in CREATE.

ballscrewbob:
Baudrate mismatch on many of the newer boards is not an issue due to the way they handle comms.

Take an MKR board and set the serial monitor to one baud rate and the board to another and it SHOULD auto adjust to give a useful output.

Only tested with earlier IDE's and not 1.8.13 and also works in CREATE.

That applies to any IDE version. It's a factor of the hardware, not the software. This is the case with the boards that have native USB capabilities, and therefore use USB CDC virtual serial ports. The primary ATmega2560 of the Arduino Mega does not have native USB capabilities, so you must match the baud rates.

This is also relevant to the discussion of the reset on port opening. The native USB boards are not reset when the port is opened. This only happens on the boards which don't have native USB capabilities, and thus have an auto reset circuit

The OP is seeing the same thing I have reported. A simple workaround for me was to use a 100 ms delay after opening the serial port. This works well to eliminate the stutter start.

Here is my code for that:

Serial.begin(115200);
while (!Serial);
delay(100);