SETUP() called more than once?

I've got a puzzling setup() problem.

It should be called once, and once only, yet I'm seeing output from the setup() function whenever I connect to the board with the Arduino Serial Monitor.

The code is below. When I deploy it to the board it works just fine. Yet if I disconnect the Monitor, wait a while, then re-connect the monitor, I get the "USB Test Harness" print from the setup() function being repeated on each re-connection.

There is definitely no redeployment at the time I re-connect the Monitor, the green LED on the board remains constantly lit, and there are no flashes of any other LED.

Any suggestions why this might be happening?


#include "NewSoftSerial.h"

const int usbrx = 2;
const int usbtx = 3;
const int usbcts = 4;
const int usbrts = 5;

NewSoftSerial usb(2,3);
char cmd[128];
char *cmdPtr;

void ProcessCommand (char *);

//      setup                                                Program initialisation

void setup() {

      // Initialise serial ports.
      Serial.begin (9600);
      usb.begin (9600);
      //lcd.begin (9600);

      // Show startup message after a pause to enable monitor connection.
      delay (5000);
      Serial.println ("USB Test Harness");
      Serial.println ("----------------");
      Serial.println ("");
      //delay (10000);

      // Initialise IO pins
      pinMode (usbrx, INPUT);
      pinMode (usbtx, OUTPUT);
      pinMode(usbrts, INPUT);
      pinMode(usbcts, OUTPUT);
      //pinMode (lcdrx, INPUT);
      //pinMode (lcdtx, OUTPUT);

      // Initialise command buffer
      cmdPtr = cmd;
      memset (cmd,0,sizeof(cmd));


//      Process                                                Main processing loop. Get commands from
//                                                            the serial port and pass to processing.

void loop () {

      // Process characters from serial port
      while (Serial.available() ) {

            *cmdPtr = ();

            // Expect commands to be terminated by CR
            if (*cmdPtr == '\r') {
                  ProcessCommand (cmd);
                  memset (cmd,0,sizeof(cmd));
                  cmdPtr = cmd;

            else ++cmdPtr;


//      ProcessCommand                                    Process a command by sending it to the
//                                                            USB drive, then printing the response.

void ProcessCommand (char *cmd) {

      char ch,lastch;

      // Log the command to the monitor
      Serial.print ("Processing command ");
      Serial.println (cmd);

      // Send the command to the USB key. Wait a few milliseconds to give the
      // USB interface time to respond.
      usb.print (cmd);
      delay (10);

      // Write any response to the monitor
      while (usb.available()) {

            // Don't allow the USB interface internal buffer to overflow.
            if (usb.available() > 8) {
                  digitalWrite(usbcts, HIGH);
            else {
                  digitalWrite(usbcts, LOW);

            ch = ();
            if (ch == 13 && lastch != 10) Serial.write (10);
            Serial.write (ch);
            lastch = ch;

      // Notify end of command.
      Serial.println ("Processing complete");
      Serial.println ("");


Answered my own question.

The following URL:

states that the monitor will reset the board when connected from Mac or Linux. I'm Windows7 64-bit, but I'm seeing the same behaviour.

This reference:

has a table which shows a reset on Windows for some boards, specifically naming Duedeci-whatever. I'm using Uno.

Rodunn, you're on the right track. The reset of the ATmega328 is connected to the DTR-line of your serial port, which gets changed when connecting to your computer. Look for how to disable this, there are a few articles out there about this.