Storing data in csv file & putting a timer to read data

Hello,

Question 1 : I'm trying to store data of 2 sensors from arduino to processing in a csv/excel file, but the data of the two sensors are stored in one column in the csv file. Instead of two.

My arduino code goes as follows :

int sensorPin = 0;

int temperatuurPin= 1;

void setup() { Serial.begin(115200); }

void loop() { int reading = analogRead(sensorPin);

int readingsecond = analogRead(temperatuurPin);

Serial.print(reading); Serial.print("\t"); //Serial.print(""); Serial.print(readingsecond); Serial.println("\t");

delay(20);

}

Which gives output data in serial monitor : |500x201

My processing code does put the serial monitor value in a csv file but only in one column : import processing.serial.*; Serial mySerial; PrintWriter output; int lf = 10; // Linefeed in ASCII void setup() { mySerial = new Serial( this, Serial.list()[3], 115200 ); output = createWriter( "log12.csv" ); } void draw() { if (mySerial.available() > 0) { String value = mySerial.readStringUntil(lf); if ( value != null ) { if (value.length() > 2) { output.print( value ); } } } }

void keyPressed() { output.flush(); // Writes the remaining data to the file output.close(); // Finishes the file exit(); // Stops the program }

I want to put the data from sensor 1 and sensor 2 in two different columns, what should I just adjust ?

Question 2 : I want to monitor the data for a long period of time, therefore I would like to have a timer that reads the data into a csv/excel file from a given start date, so day , hour and minutes and the readings should stop for a given end data: day,hour, minutes.

Greetings

CSV? As in COMMA separated values. What happens if you put a comma between them instead of a tab?

bn1234: Question 2 : I want to monitor the data for a long period of time, therefore I would like to have a timer that reads the data in a csv file from a given start date, so day , hour and minutes and the readings should stop for a given end data: day,hour, minutes.

Greetings

reading data from the csv? or putting data into the csv? If you want to read data out from the csv and know anything about the time it was put in there then you'll need to put the time as well. If you mean writing to the csv for a limited period of time then please make that clear.

bn1234:
reading into csv, my bad

You don’t read into a file. You can write into a file or read from a file. What do you want to do?

write into a file

Do you have a RTC chip or something to tell Arduino what time it is? You'll need that if it is to be by date and real time (like Nov 1, 12:00 to Nov 2, 3:00). If you just want a given time interval (like do it for 2 hours) then Arduino can time that itself.

How are the start and end times entered?

I have not an RTC chip yet, I would like to know both ways. But as for now you can explain the second way. The start and end times are entered manually.

bn1234:
But as for now you can explain the second way.
The start and end times are entered manually.

The first way has start and end times. The second way only has an interval. Which one are you referring to?

Look at the “Blink Without Delay” example for some inspiration on timing things for an interval. You’ll record the value of millis() when you start and compare at each iteration with the new value returned from millis() to see how long it has been.

For example:

unsigned long startTime;

void setup(){
     startTime = millis();
}

void loop(){
     if(millis() - startTime <= 10000){
          // code won't get here after ten seconds
     }
}

Why is an RTC necessary, why can't I put a timer in processing script.

bn1234: Why is an RTC necessary, why can't I put a timer in processing script.

It's not. But if you want Arduino to know the absolute time, something has to be able to tell it what time it is at least once.

DO you mean you want the Arduino to constantly send and the processing app to only record for a given amount of time?

yes,if data that is received in processing from arduino is synchronous. What would be the best to send data from arduino continuously and record in processing for a given amount of time or to send data from arduino within a timeframe?

bn1234: yes,if data that is received in processing from arduino is synchronous. What would be the best to send data from arduino continuously and record in processing for a given amount of time or to send data from arduino within a timeframe?

That depends on what you have and how you want to code it. It could be done either way. If this is all Arduino is intended to do then I would be tempted to have it sending continuously and let processing ignore it when outside the time window. But if Arduino has other things to take care of and this is but one part of his job then maybe the timing should go on Arduino.

Yes, the arduino is only intended to capture sensor data, so I need a timer in processing.

bn1234: Yes, the arduino is only intended to capture sensor data, so I need timer in processing.

OK, my guess is that better help lives in the processing forums. I don't know much about processing. I usually write the PC side of my projects in Python or regular Java.

Arduino side (changed to 10 samples / sec, single ';' delimiter)

const byte sensorPin = A0;
const byte temperatuurPin = A1;

void setup() {
  Serial.begin(115200);
}

void loop() {
  int reading = analogRead(sensorPin);
  int readingsecond = analogRead(temperatuurPin);
  Serial.print(reading);
  Serial.print(";");
  Serial.println(readingsecond);
  delay(100);
}

Processing side (added time as first column, followed by two Arduino columns, ";" field seperator)

import processing.serial.*;
Serial mySerial;
PrintWriter output;
int lf = 10;    // Linefeed in ASCII
void setup() {
  printArray(Serial.list());
  mySerial = new Serial( this, Serial.list()[3], 115200 );
  output = createWriter( "log12.csv" );
  mySerial.bufferUntil(lf);
}
void draw() {
}
void serialEvent(Serial p) {
  String rStr = p.readString();
  if ( rStr != null ) {
    if (rStr.length() > 2) {
      String[] fld = splitTokens(rStr, ";");
      if (fld.length>=2) {
        output.print(year());
        output.print(".");
        output.print(month());
        output.print(".");
        output.print(day() );
        output.print("-");
        output.print(hour() );
        output.print(":");
        output.print(minute());
        output.print(":");
        output.print(second());
        output.print("-");
        output.print(millis());
        output.print(";");
        output.print(fld[0]);
        output.print(";");
        output.println(fld[1]);
      }
    }
  }
}
void keyPressed() {
  output.flush();  // Writes the remaining data to the file
  output.close();  // Finishes the file
  exit();  // Stops the program
}

Output

2015.11.1-20:59:47-1698;87;0
2015.11.1-20:59:47-1784;94;0
2015.11.1-20:59:47-1885;101;0
2015.11.1-20:59:47-1985;109;0
2015.11.1-20:59:47-2085;115;0
2015.11.1-20:59:47-2185;122;0
2015.11.1-20:59:48-2286;127;0
2015.11.1-20:59:48-2386;132;0
2015.11.1-20:59:48-2487;136;0
2015.11.1-20:59:48-2587;139;0
2015.11.1-20:59:48-2687;141;0
2015.11.1-20:59:48-2788;142;0
2015.11.1-20:59:48-2888;142;0

thanks