help with serial and counter

Hello

Here´s my simple noob code and my simple noob problem:

#include <SoftwareSerial.h>

int cont = 0;

void setup(){

//setup for Serial port
Serial.begin(19200);

}
void loop(){
delay(1000);
Serial.print(cont);
cont = cont + 1;

}

Each time I connect to serial the value of “cont” starts at 0, then 1, 2, 3… if i connect “X” times the value of “cont” starts at “0” “X” times. What should i do to retain the value of “cont” independent of the serial connection???

Tks
Skyport

Looks like the arduino is resetting each time you connect to serial. To maintain the value, you can write it to EEPROM after each change, and read it from EEPROM on startup (in your setup method). The other solution would be to somehow not make the arduino reset on serial connect, by I have no idea how to accomplish that.

http://arduino.cc/en/Reference/EEPROM explains reading from/writing to eeprom.

Note that eeprom does have a certain number of times it can be written to per cell... somewhere in the hundreds of thousands mind you, but there is a failure point for each cell. If you are using it for a rapidly updating counter, that might be a problem.

Any other solution beyond writing it to the EEPROM?

Any other solution beyond writing it to the EEPROM?

The question is, what are you trying to do. As your program stands now, it doesn't really do anything useful. Depending on your application, you can work around the limitations of the EEPROM, but again for that we would need to know what problem you're trying to solve.

If your application is just this program exactly as it stands, the answer will be no.

Korman

You could use an external flash-chip, but those cells also go bad after enough writes.

I´m trying to write to a txt file on a computer a sequence of photographs and coordinates (long, lat). For each shot it stores the number of the shots and the coordinates. The frame rate is about 1 per 3 seconds. I could use a micro sd card and connect it to the board, but it´s more hardware to implement. I would prefer to store the data to a file in the computer. I´m storing the images also at the computer. The triggering for the camera it´s easy and already done wiht the arduino. The images are stored at the pc each time the camera is triggered. The camera is a Nikon D90 and the software to store the images is DCamCapture but this is not important.

Tks

Skyport

Or simply disable auto reset.

For this application, you probably can use the EEPROM, it'll last longer than the shutter on your camera. But you have exactly 512 bytes of EEPROM. Will that be enough for what you do?

Korman

Just an FYI- the 168 has 512 bytes, but the ATMEGA328 has 1k EEPROM (and yes you can use it). The docs reflect the 168 configuration.

You could also store the current number in a file on the PC and have the Arduino sketch request the number from the PC at startup.

Regards,

-Mike

I think that Groove could be on to something....

The 120 ohm resistor worked just fine.

http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

Tks

Skyport