Serial Moniter changing variables

Hi this is my code

int y = 0;
int x = 0;
void setup(){
  Serial.begin(9600);
}
void loop(){
  delay(1000);
  y = y+1;
  x = y;
  Serial.print(x);
    Serial.println();
}

Its supposed to count 1 2 3 ... On the serial moniter. It does, but if the serial moniter gets turned off, the count starts from 0. I want to know if there is a way if the arduino is powered by a battery and unhooked from the computer or if just the serial moniter gets shut down, if arduino can keep counting and when it gets attached the computer again or the serial moniter gets brought up, the count would be at however many seconds after it first started counting. I thought by having x and y, that the serial moniter would would only change x, the variable printed, but that didn't work. Any help would be appreciated.

Opening or closing the serial monitor causes the board to reset. You can use another terminal program and avoid that.

Delta_G: Opening or closing the serial monitor causes the board to reset. You can use another terminal program and avoid that.

You can also use a resistor or capacitor to defeat the reseting of the arduino when the serial monitor is opened or closed.

Well, I don’t know much. I found http://www.circuitsathome.com/mcu/android-adk-terminal-emulator-for-arduino, a terminal program
Also which capacitator or resister would do the job and how would I wire it?
My final goal with this is a device with a arduino and a ping ultrasonic sensor. The program would get the time (or count seconds), get disconnected from the computer, record the time every time something came in range of the ping and once connected to the computer and the serial moniter is brought up, write down the times. I will work more time setting commands.

Also which capacitator or resister would do the job and how would I wire it?

I've used a 100ohm resistor between the +5v pin and the reset pin. Others have used a 5uf capacitor between the reset pin and ground. This prevents the reset pin being momentarly being pulled low (initiating the board reset) when ths serial port opens. Note that these will need to be removed when uploading code to the arduino.

Thanks so much! :grin: Attaching a 2.2uf capacitator between reset and 5v did the trick. Now I will start working on the code for Ping. Thanks! :grin:

Well, as I suspected, I had a problem.
My code is

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* +V connection of the PING))) attached to +5V
	* GND connection of the PING))) attached to ground
	* SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe
 
   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:


int x = 0;
int y = 0;
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  
  delay(100);
  
    if(inches < 30){
  delay(1000);
  y = y+1;
  x = y;
  Serial.print(x);
    Serial.println();
  }
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

I got it from the example code for the Ping ultrasonic sensor and combined it from the counting script. Its supposed to only write down the number on the serial moniter when the Ping sees something less that 30 inches away. If the serial moniter isn’t available or the arduino isn’t connected to the computer, its supposed to record the count at that moment and write it down on the serial moniter when it can(not built into the script yet). Programming help would be good.

I've never seen any technique for detecting whether there's anything at the other end of the serial interface on an arduino without having the other side send something to say so. That's not to say that there isn't of course, but generally the arduino is perfectly happy to spray data out of the serial port blissfully unaware that nothing is listening. Even if you find a way though, are you planning to store all the readings that weren't sent? You'll likely need somewhere to store it external to the arduino or you'll quickly run out of memory.