Why do parameters reset on Serial Connection?

Hi,
I have tried connecting and reconnection the serial monitor whilst run a sketch on the Arduino. For some reason the registers and parameters all reset on every connection. (On the built in serial monitor and HyperTerminal)
Can anyone suggest why please?
As well as using my own variable which reset, millis() and micro() also reset.
The sketch code is below, All suggestions taken!

Thanks

Pete

#include <MsTimer2.h>

unsigned long time;
unsigned long time1 = 0;
unsigned long time_diff;
unsigned long micro;

long counter = 0;

// Switch on LED on pin 13 each second

void flash() {
static boolean output = HIGH;
digitalWrite(13, output);
output = !output;
counter++;
}

void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
MsTimer2::set(1, flash); // 500ms period
MsTimer2::start();
}
void loop(){
Serial.print("Time: ");
time1 = time;
time = millis();
time_diff = time - time1;
micro = micros();
//prints time since program started
Serial.print(time);
Serial.print("\t");
Serial.print(time1);
Serial.print("\t");
Serial.print(time_diff);
Serial.print("\t");
Serial.print(counter);
Serial.print("\t");
Serial.print(micro);
Serial.println();

// wait a second so as not to send massive amounts of data
delay(100);
}

It's a "feature". When the Serial Monitor window is opened, the main function in the generated cpp file is called, which calls setup and loop.

Since millis is reporting the time since the setup function was called, it starts over.

I don't know whether this is the intended behavior, or not, but it is the behavior that occurs.

As soon as you start the serial monitor the arduino starts with the setup in your sketch. I guess that's an intended behavior.

The action of opening up the serial monitor in the Arduino IDE causes the DTR signal to reset the AVR processor via the auto-reset feature on the Arduino board.

This of course causes the program to start from the beginning with all variables initialized, just as if you pressed the reset button on your Arduino.

Lefty

Hi milneandfletch,

to avoid the auto-reset via the DTR signal you have two options:
A) put a 120 Ohm resistor between +5V and RESET
B) cut the wire between the RESET-EN jumper and replace it by a switch

Before you do any of the changes you should know that auto-reset is needed to upload new sketches because it activates the boot loader so you don't want to disable auto-reset permanently.

MikeT

Hi,
Thanks for your reply!
MikeT - I will look into replacing it with a switch!
Can anyone point me in the direction of a PCB layout diagram for the Arduino 2009 and Diecimila model?
Thanks

Pete

Hi milneandfletch,

You can simply find the schematics and Eagle board layout at the Arduino Hardware page: http://arduino.cc/en/Main/Hardware

MikeT