Setup running twice

I know this has come up before but this is different and all the threads are locked.

Here is my code,


//uint32_t var;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
  Serial.println ();
  //var++;
  Serial.print("Hello World!  ,");
  Serial.println(millis());
}

void loop() {
  // put your main code here, to run repeatedly:

}

It runs on an Arduino Nano. Nothing connected, just the Nano.
I put the Serial.print in to try and figure out what is happening.
I clicked on the code tag but not sure if it worked.

I remmed out the variable (uint32_t) to check it wasn't that. Weird thing is, if I change the delay to 1000 it stops it happening.

BTW, I'm running Linux Mint Cinnamon 22.1. And using Arduino IDE 2.3.10

This is what I see,

Is there anything else I can post to help?

If you simply press the reset button with the Serial monitor already open, what happens ?

Opening the Serial monitor resets the Nano.

only see a single print using an UNO

that the time is the same means a reset is occuring, not that setup( ) is running twice.

Try replacing your setup with this:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {}
  Serial.println ();
  //var++;
  Serial.print("Hello World!  ,");
  Serial.println(millis());
}

Serial takes time to initialize. Adding while (!Serial) {} stops the code until Serial is initialized.

This used to happen to me in IDE 1.x. Sugestion was a 10uf capacitor on reset pin/gnd. I did not use the suggestion because the IDE release solved my issue.

which is fine is it does eventually finish or open or whatever.

Wrap that test in a timer so the code will just get on with it

Serial.begin(115200);

unsigned long timeout = millis() + 2000;
while (!Serial && millis() < timeout) {
}

Serial.println("Starting...");

If it times out, serial printing goes nowhere and serial input will report 0 chars available or -1 if you read one.

Or just skip the test and live with the first printing to be missed.

Different boards are, um, different. On many, there is no need to wait.

Worst: the sketch hangs until someone on the OC side opens the port. Tough to deploy that.

a7

On a Nano while(!Serial) has absolutely no function; it's only useful on boards that use native USB.

It worked. But you also placed your text inside the code tags :wink:

You're probably using the forum editor in "rich text editor" mode and you'll have to get out of the code "window" by using the arrow-down.

Alternatively, select the markdown mode by toggling the M+|A in the left top. The editor will then look like below; you type in the left and the right shows what it will look like.

How is it different?

what would cause multiple resets .........?

Serial Monitor restarting, for one.

why would the Serial monitor run again after it was already running and displayed the original print and time?

In '21, it was explained to me; when the IDE started Serial Monitor, or recognised a USB connection, /RESET (I think "NOT"ed) line was effected, so I should tie /RESET with a capacitor to ground so the effect would be smoothed. I opted to watch it double-start until the next IDE release.

I strongly suspect buffering by the operating system.

After an upload the processor is reset and sends the data to the computer (PC). If you open serial monitor after that another reset occurs and data is again send to the computer.

You can upload while serial monitor is open (does not work in IDE 2.x with an original Nano that uses the FT232) or you can close and re-open serial monitor or (as mentioned in post #5 by @Deva_Rishi) give the board a manual reset.
All three scenarios should result in a single message.

this sounds more like switch bounce that should take just a few msec so you wouldn't see a print after a 500 msec delay and then see another restart.

yes, what happens?

Not a switchbounce. Starting Serial Monitor. Even a delay before Serial.begin(); would cause a double reset.

void setup() {
  delay(anyLargeValue);
  Serial.begin(115200);
  Serial.print("Hello, World!");
}

yes,
This happens because your computer sends a DTR (Data Terminal Ready) signal when the serial connection opens. This signal momentarily pulls the board's reset pin low, triggering the microcontroller to restart your sketch from the beginning

This occurs when the Serial monitor is opened and connects the com port, not when Serial.begin() is run

using a capacitor to prevent a 2nd reset suggests that the 2nd reset occurs very quickly (few msec)

what i see with following code executing Serial.begin () multiple times

111 msec
1259 msec
2470 msec
const byte PinBut = A1;

void loop()
{
    pinMode (PinBut, INPUT_PULLUP);

    if (LOW == digitalRead (PinBut))  {
        Serial.begin   (9600);
        Serial.print   (millis ());
        Serial.println (" msec");
        delay (500);
    }
}

void setup ()
{
}