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
}
}