println() double print

Dear Colleagues,

I am brand new with Arduino and therefore, started with a very simple project just to print out into the Monitor a single int value. My board is nano (version with old bootloader). The problem is println() and print() both print the value twice. The screenshot attached below.
I can’t figure it out and seek your help and advice.

Thanks.

Please post your code.

It is shown in attached file.

void setup()
{
int ObstacleDetected = 1;
Serial.begin(9600);
Serial.println(ObstacleDetected);

}

void loop()
{

}

I can’t see an attached file, and I can’t edit code in an image.

Are you saying that 1 is printed twice?

Your processor is probably getting reset.

Please remember to use code tags when posting code.

Every time the Arduino is reset it will print once. The Arduino is automatically reset when uploading new sketches. Possibly sometimes more than once...

Try...

Serial.println("Started...");
Serial.println(ObstacleDetected);

It should be obvious the code is only printing ONCE per reset.

Sorry Mate, first post. Surely, I should use code tags with code snippet.

And yes, it does print value 1 twice. I was thinking about reset but can't understand why would board reset as nothing is connected to it. Also, print happens almost momentarily, as function gets called twice.
I wonder why can't you open attached file? It opens OK at my side.

I am a bit frustrated as it is so simple issue and yet I cannot solve it unless, it is Arduino specific, such as not explicitly initialised ports or similar, perhaps?

Inserted Serial.println("Started..."); as Pcbbc suggested. It is obvious now that setup() called twice, due to reset, perhaps.

Started...
1
Started...
1

Now, why would the board reset? Do I really need to explicitly initialise all ports or something else?

You computer resets it. Either...
a) When programming
b) When connecting the serial monitor (at least in my experience)

If you don't want it to reset, disconnect the DTR line from the programmer. *
Note that you will then need to manually reset it with the reset button (at the correct point) when programming.

  • Edit only an option if you have an external USB to TTL serial programmer. If you have a built in USB, there may be an option link on hte board to disable the DTR reset. If not you can add a capacitor. Link

But in real world applications (without the computer attached) the double reboot won't happen. Please explain why it is a problem when testing.

The most likely scenario is that after uploading or plugging in the Arduino, the program starts, prints everything once, the output is buffered somewhere (in the USB-to-Serial chip or in your OS's buffer). Then you open the serial port in the IDE, this causes the DTR line to go low, which resets the ATmega, so it executes the program again, printing everything a second time.

When the serial monitor reads from the port, it shows the buffered data from before you opened the port.

Pieter

Thank you guys. It is that I am coming from PC programming and that is why it is quite weird behaviour for me.

Pieter's scenario looked more likely to me. So, I've tried to manually reset the board (after it has been run once and printed twice :slight_smile: ) and it does print twice again! everytime I push the reset button, it prints double value.
What I mean, the board is connected, sketch is uploaded, program runs - prints twice. OK, then I manually reset with the board button and it prints twice, again. I guess, it should not be anything coming from PC side or sitting in the buffer (I supposed buffer purged automatically after print() returns).

Pcbbc scenario b) tells me that when the serial monitor connects, the board resets. This looks like a reasonable explanation for this double print and looks like a board specific behaviour.

It would be interesting to see how board behaves in real world scenario. I will need to get a proximity sensor to hook up and test.

Much appreciated!