Saving data and timestamps to csv (noob)

Hello!

I’m trying to save data from my Arduino to a csv file using Processing. I’ve successfully saved the raw data (one number at a time) to a txt file, but my goal is to save both that data and timestamps to a csv. I know this is such a basic question, but I’ve tried about 10 tutorials/threads and haven’t been able to write to a csv yet.

I have a few questions:

  1. When I managed to save data to the txt file, I did that in draw(). Other threads said to leave draw() blank and use serialEvent(). But I haven’t been able to use serialEvent() to write data to files.
  2. I’m not sure if I need to make a table object in order to save to the csv.

Thanks for your time.

Seriously, you should not be doing the saving in draw().

The draw() function runs 60 times a second. There is no need to save your data that often.

You only need to save the new data once when there is new data. And when there is new data, you get it in serialEvent().

So, YES, DO THE SAVING IN serialEvent()!


Saving a .csv file is basically the same as saving a .txt file. You just have to make the filename end with .csv instead of .txt. You also need to put commas in between things.

Concatinating strings is simple: String toSave = "" + timeStamp() + ", " + data;


Anyway, the biggest problem you have is a simple one: You are asking for help and you haven’t posted the code you have! POST YOUR CODE!

The next problem is to make sure your code is formatted on the forums. Check. It’s not, right? Edit your post, select the code, and hit the FORMAT YOUR CODE button, which looks like this: </>

2 Likes

Thanks for your detailed reply, @TfGuy44!

This is my code for writing to a text file, and I’m currently trying to figure out what should go in draw() and what else I should move to serialEvent(). Should anything go into draw()?

import processing.serial.*;
Serial mySerial;
PrintWriter output;

void setup() {
   mySerial = new Serial( this, Serial.list()[3], 9600 );
   output = createWriter( "data.txt" );
}

void draw() {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readStringUntil('\n');
         println(value);
         if ( value != null ) {
              output.println( value );
         }
    }
}

void keyPressed() {
    output.flush();  
    output.close(); 
    exit();  
}

Should it be an if or while statement in serialEvent()? Also, do I need the port.bufferUntil() method in setup()? This link says I do.

Currently my code looks like this, but I’m not writing the data correctly because the csv file is empty.

import processing.serial.*;
Serial mySerial;
PrintWriter output;

void setup() {
   mySerial = new Serial( this, Serial.list()[3], 9600 );
   output = createWriter( "dat2.csv" );
//   mySerial.bufferUntil('\n'); 
}

void draw() {
}

void serialEvent(Serial mySerial) {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readStringUntil('\n');

         println(value);
         if ( value != null ) {
           String timestamp = str(hour()) + str(minute()) + str(second());
           value = "" + timestamp + ", " + trim(value);
           output.println( value );
         }
    }
}

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