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

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

//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.

1 Like

try

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/nf_.html

shows like

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

1 Like
2 Likes