# Saving data and timestamps to csv (noob)

**URL:** <https://discourse.processing.org/t/saving-data-and-timestamps-to-csv-noob/2674>\
**Category:** Coding Questions\
**Created:** [August 16, 2018, 12:12am UTC](https://discourse.processing.org/t/saving-data-and-timestamps-to-csv-noob/2674 "2018-08-16T00:12:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ripple18](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@ripple18](https://discourse.processing.org/u/ripple18)\
**Post date:** [August 16, 2018, 12:12am UTC](https://discourse.processing.org/t/saving-data-and-timestamps-to-csv-noob/2674/1 "2018-08-16T00:12:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 16, 2018, 12:29am UTC](https://discourse.processing.org/t/saving-data-and-timestamps-to-csv-noob/2674/2 "2018-08-16T00:29:40Z")

</div>

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: \</\>

---

<div class="post-metadata">

**Author:** ![ripple18](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@ripple18](https://discourse.processing.org/u/ripple18)\
**Post date:** [August 16, 2018, 5:05am UTC](https://discourse.processing.org/t/saving-data-and-timestamps-to-csv-noob/2674/3 "2018-08-16T05:05:56Z")

</div>

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()?

```auto
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();  
}

```

---

<div class="post-metadata">

**Author:** ![ripple18](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@ripple18](https://discourse.processing.org/u/ripple18)\
**Post date:** [August 16, 2018, 6:56pm UTC](https://discourse.processing.org/t/saving-data-and-timestamps-to-csv-noob/2674/4 "2018-08-16T18:56:42Z")

</div>

Should it be an if or while statement in serialEvent()? Also, do I need the port.bufferUntil() method in setup()? [This link](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing) says I do.

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

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

```
