# Save data from Arduino to PC in CSV file in real time by Processing code

**URL:** https://discourse.processing.org/t/save-data-from-arduino-to-pc-in-csv-file-in-real-time-by-processing-code/12959
**Category:** Electronics (Arduino, etc.)
**Created:** [July 24, 2019, 9:00pm UTC](https://discourse.processing.org/t/save-data-from-arduino-to-pc-in-csv-file-in-real-time-by-processing-code/12959 "2019-07-24T21:00:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hectorta](https://avatars.discourse-cdn.com/v4/letter/h/a87d85/32.png) [@hectorta](https://discourse.processing.org/u/hectorta)
#### Post date: [July 24, 2019, 9:00pm UTC](https://discourse.processing.org/t/save-data-from-arduino-to-pc-in-csv-file-in-real-time-by-processing-code/12959/1 "2019-07-24T21:00:46Z")

</div>

I want to generate file name and data & time in csv file in the format yyyymmdd hhmmss.zzz. Where: zzz is millisecond. e.g 20190709 090801.001. But my following code can only get the format 201979 981.1

```auto
//import the required libraries
import processing.serial.*;

Serial mySerial;
Table table;
String filename;

void setup()
{
  //set mySerial to listen on COM port 10 at 9600 baud
  mySerial = new Serial(this, "COM4", 115200);

  table = new Table();
  //add a column header "Time" and "Date" for a timestamp to each data entry
  table.addColumn("Date");
  table.addColumn("Time");
  //add a column header "Data" for the collected data
  table.addColumn("Temperature Channel 0");      
}

void draw()
{
  //variables called each time a new data entry is received
  int d = day();  
  int m = month();
  int y = year();
  int h = hour();
  int min = minute();
  int s = second();

  if(mySerial.available() > 0)
  {
    //set the value recieved as a String
    String value = mySerial.readString();
    //check to make sure there is a value
    if(value != null)
    {
      //add a new row for each value
      TableRow newRow = table.addRow();
      //place the new row and value under the "Data" column
      newRow.setString("Data", value);
      //place the new row and time under the "Time" column
      newRow.setString("Time", str(h) + ":" + str(min) + ":" + str(s));
      //place the new row and date under the "Date" column
      newRow.setString("Date", str(d) + "/" + str(m) + "/" + str(y));
    }
  }
}

void keyPressed()
{
  //variables used for the filename timestamp
  int d = day();
  int m = month();
  int h = hour();
  int min = minute();
  int s = second();
  //variable as string under the data folder set as (mm-dd--hh-min-s.csv)
  filename = "data/" + str(m) + "-" + str(d) + "--" + str(h) + "-" + str(min) + "-" + str(s) + ".csv";
  //save as a table in csv format(data/table - data folder name table)
  saveTable(table, filename);
  exit();
}

```

​ ​I handled this task easily in C++ by using setfill and setw commands. But I haven’t used Processing code before, so I don’t know how to fill in the blank space, such as hour, minute less than 10 your code only shows 1 digit. Please tell me how to make the format with full digits. For example, if the hour is 1h, I want it to generate 01, not 1. ​ ​Besides, after generated the CSV file, all data are put in the same row with date & time. It should be each data corresponding a single second. I want it to update data every second for 30 sensor channels. How to make it work by Processing code? ​ ​Thanks in advance.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 25, 2019, 2:19am UTC](https://discourse.processing.org/t/save-data-from-arduino-to-pc-in-csv-file-in-real-time-by-processing-code/12959/2 "2019-07-25T02:19:36Z")

</div>

try

```auto
String filename;

void setup() {}
void draw() {}
void keyPressed() { //variables used for the filename timestamp
  //variable as string under the data folder set as data/yyyy-mm-dd--hh-min-sec_millis.csv
  filename = "data/" + year()+"-"+ nf(month(),2,0) +"-"+ nf(day(),2,0)+"--"+ nf(hour(),2,0) +"-"+ nf(minute(),2,0) +"-" + nf(second(),2,0) +"_"+millis() + ".csv";
  // please note that millis() is the milli seconds since start of sketch!
  println(filename);
}

```

[https://processing.org/reference/millis\_.html](https://processing.org/reference/millis_.html)

[https://processing.org/reference/nf\_.html](https://processing.org/reference/nf_.html)

shows like

```auto
data/2019-07-25--09-18-50_9401.csv
data/2019-07-25--09-18-51_10340.csv
data/2019-07-25--09-18-52_11138.csv

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 25, 2019, 4:41am UTC](https://discourse.processing.org/t/save-data-from-arduino-to-pc-in-csv-file-in-real-time-by-processing-code/12959/3 "2019-07-25T04:41:20Z")

</div>

> [@Timer with specific format](https://discourse.processing.org/t/timer-with-specific-format/6864/3):
>
> nf() -\> [Processing.org/reference/nf\_.html](http://Processing.org/reference/nf_.html)[Forum.Processing.org/two/discussion/16001/add-0-s-to-date-time-in-the-filename-with-saveframe#Item\_3](http://Forum.Processing.org/two/discussion/16001/add-0-s-to-date-time-in-the-filename-with-saveframe#Item_3) SimpleDateFormat -\> [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html)[Forum.Processing.org/two/discussion/10067/how-to-prevent-file-overwriting#Item\_3](http://Forum.Processing.org/two/discussion/10067/how-to-prevent-file-overwriting#Item_3)
