collecting data in arduino and then temporary connect to PC to upload

for counting a sequence of pulses I use an arduino uno that counts pulses via pin2 at every n pulses, for example 1000 it stores the time in an internal array. in this way I want to collect data for a period of several days or weeks. then at regular times I want to connect the arduino to my laptop via serial connection and then I sent a receive command from the laptop program and then the arduino sends back the array as far as filled.

this works fine as long as I let the arduino be connected with the laptop. when I disconnect (with external supply connected to Arduino) the arduino continues collecting pulses as i csn conclude from the leds connected, however when reconnecting to the laptop and sensing a command it seems that all data stored in the array is lost. it looks as if arduino start from scratch cunting pulses.

replacing the laptop program by Arduino serial monitor and sending the sent command it also works, as i cab see the arduino sending the data, however also here the array seems to be lost when closing the serial monitot and starting it again.

not clear to me what happens here. probably the reconnecting makes the Arduino startup again from setup?

What am I doing wrong?

/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.

The circuit:

  • LED attached from pin 13 to ground

  • pushbutton attached to pin 2 from +5V

  • 10K resistor attached to pin 2 from ground

  • Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

created 2005
by DojoDave http://www.0j0.org
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int countPin = 10;
// variables will change:
int buttonState = 0;
signed int n=0;
bool state =false;
bool interval=false;
byte command=0;
int avail=0;
int data1=0;
// variable for reading the pushbutton status
signed int teller ;
signed int timearray[600];
word timepointer=0;

void setup() {
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(countPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
teller=0;
digitalWrite(countPin, LOW);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
avail = Serial.available();
if (avail>0) // karakter verzonden z=zenden, r=alle meetdata verwijderen
{ for (n=0; n<avail; n++) command=Serial.read();
if (command=='z') //zenden
{ command=0; //reset command
for (n=0; n<timepointer; n++)
{Serial.print(timearray[n]);
Serial.print(' ');

}//25 moet naar 600
}
if (command=='r') //reset hele serie
{for (n=0; n<600; n++)timearray[n]=0;
timepointer=0; //terug naar 1e punt
}
}
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
state=true;
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
if (state==true) {teller++; state=false;};
delay(1);
}
if (teller==10) //10=aantal koolzuurpulsen per meting
{teller=0;
interval=not interval;
digitalWrite(countPin, interval);
timearray[timepointer]=round(millis()/1000); //seconden
timepointer++;
if (timepointer>600) timepointer=600; //max 600 punten

}
}

When the PC opens the serial port it causes the Arduino to reset - this is the normal behaviour.

The simplest solution (IMHO) is to use a USB-TTL cable and connect directly to the Arduino Rx Tx and GND pins rather that using the regular USB connector.

When the PC opens the serial port on the USB-TTL cable it will not cause the Arduino to reset.

There are methods to disable the auto-reset process - Google should be able to find them for you.

...R

ok thanks a lot. searching on 'disable the auto-reset' I found that it can be prevented by a simple hardware change in the board. (I am an electronics engineer so that should not be the issue) However I assume there should be a good reason to do this reset? because resetting is basically a disadvantage because it makes it impossible to store data in a standalone application and then afterwards upload it to a PC, thereby limiting the possibilities of the arduino. so can I do this without causing other issues?

I tried a solution with 1uF cap (no elcap) between 5V and reset pin to prevent reset at USB open from laptop. (cap removed when powerup de Arduino) issue solved.

If it was me I would forget to put the capacitor on until I had lost my data :slight_smile:

...R

hanshalber:
I assume there should be a good reason to do this reset?

The reason for the auto-reset circuitry is uploading. There is a special program called a bootloader in a dedicated section of the flash memory on you Uno's ATmeg328P microcontroller. This program runs whenever the microcontroller resets or is powered on. The bootloader on your ATmega328P listens a little while for an upload to start over the serial port. If the upload starts, the bootloader writes the incoming data to flash memory. If the upload doesn't start, the bootloader times out and exits to whatever code is already loaded on the microcontroller in the application section of memory.

In the early days of Arduino, you would need to manually press the reset button at just the right time during the upload (after the compilation finished, but before the uploader program timed out). Then someone figured out that a very simple circuit could be added to the Arduino boards to make them automatically reset at just the right time, which is much more convenient and beginner friendly. The way this auto reset circuit works is to reset the microcontroller whenever a serial connection is opened on the Uno's virtual COM port.

hanshalber:
so can I do this without causing other issues?

As you might have guessed by now, after disabling the auto-reset circuit you will now need to either manually reset the microcontroller at just the right time during the upload process or else re-enable the reset circuit before uploading (and then remember to disable it again after).

To do the manual reset, watch the black console window at the bottom of the Arduino IDE window during the upload. Press and release the reset button as soon as you see something like this:

Sketch uses 444 bytes (1%) of program storage space. Maximum is 32256 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.