# Saving data in Processing

**URL:** https://discourse.processing.org/t/saving-data-in-processing/32136
**Category:** Electronics (Arduino, etc.)
**Created:** [September 7, 2021, 3:07pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136 "2021-09-07T15:07:44Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![nyeas001](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nyeas001](https://discourse.processing.org/u/nyeas001)
#### Post date: [September 7, 2021, 3:07pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/1 "2021-09-07T15:07:44Z")

</div>

Hi,  
I am trying to save data in a table in processing. Data is pressure sensor data collected through serial monitor of Arduino. I am getting a error message which I could not resolve.

Code

```auto
import processing.serial.*;
Serial myPort; // Create object from Serial class
//String val; // Data received from the serial port
Table dataTable;
//Table table;
//Table table;
table = new Table();

int numReadings = 5000; //keeps track of how many readings you'd like to take before writing the file. 
int readingCounter = 0; //counts each reading to compare to numReadings. 
 
String fileName;

void setup(){
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  table.addColumn("id"); //This column stores a unique identifier for each record. We will just count up from 0 - so your first reading will be ID 0, your second will be ID 1, etc. 
  
  table.addColumn("year");
  table.addColumn("month");
  table.addColumn("day");
  table.addColumn("hour");
  table.addColumn("minute");
  table.addColumn("second");
  table.addColumn("Load cell"); //the following are dummy columns for each data value. Add as many columns as you have data values. Customize the names as needed. Make sure they are in the same order as the order that Arduino is sending them!
 }

 
void serialEvent(Serial myPort){
  val = myPort.readStringUntil('\n'); //The newline separator separates each Arduino loop. We will parse the data by each newline separator. 
  if (val!= null) { //We have a reading! Record it.
    val = trim(val); //gets rid of any whitespace or Unicode nonbreakable space
    println(val); //Optional, useful for debugging. If you see this, you know data is being sent. Delete if you like. 
    float sensorVals[] = float(split(val, ',')); //parses the packet from Arduino and places the values into the sensorVals array. I am assuming floats. Change the data type to match the datatype coming from Arduino. 
   
    TableRow newRow = dataTable.addRow(); //add a row for this new reading
    newRow.setInt("id", table.lastRowIndex()); //record a unique identifier (the row's index)
    
    //record time stamp
    newRow.setInt("year", year());
    newRow.setInt("month", month());
    newRow.setInt("day", day());
    newRow.setInt("hour", hour());
    newRow.setInt("minute", minute());
    newRow.setInt("second", second());
    
    //record sensor information. Customize the names so they match your sensor column names. 
    newRow.setFloat("sensor1", sensorVals[0]);
    //newRow.setFloat("sensor2", sensorVals[1]);
    
    readingCounter++; //optional, use if you'd like to write your file every numReadings reading cycles
    
    //saves the table as a csv in the same folder as the sketch every numReadings. 
    if (readingCounter % numReadings ==0) //The % is a modulus, a math operator that signifies remainder after division. The if statement checks if readingCounter is a multiple of numReadings (the remainder of readingCounter/numReadings is 0)
    {
      fileName = str(year()) + str(month()) + str(day()) + str(dataTable.lastRowIndex()); //this filename is of the form year+month+day+readingCounter
      saveTable(dataTable, fileName); //Woo! save it to your computer. It is ready for all your spreadsheet dreams. 
     }
   }
}
void draw() {
  if (mousePressed == true) 
  { //if we clicked in the window
   myPort.write('T'); //send a 1
   println("T");   
  } 
  else 
  { //otherwise
  myPort.write('0'); //send a 0
  }   
}

```

I am getting the error message: Syntax Error - Missing operator, semicolon, or ‘}’ near ‘setup’?  
But I could not find any missing semicolon, or ‘}’ near ‘setup’. Where exactly I am doing wrong? Could anyone please help?

---

<div class="post-metadata">

### Author: ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)
#### Post date: [September 7, 2021, 3:26pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/2 "2021-09-07T15:26:12Z")

</div>

Hi @nyeas001

You have to initialize the variable table in setup, not before.  
I cannot have these as you have:

```auto
//String val; // Data received from the serial port
Table dataTable;
//Table table;
//Table table;
table = new Table();

```

I uncommented the String val, and the Table table. I initialized the table = new Table() in setup as you can see.  
Hope it works!

```auto
import processing.serial.*;
Serial myPort; // Create object from Serial class

Table dataTable;
String val; // Data received from the serial port
Table table;

int numReadings = 5000; //keeps track of how many readings you'd like to take before writing the file. 
int readingCounter = 0; //counts each reading to compare to numReadings. 
 
String fileName;

void setup(){
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial(this, portName, 9600);
  
  table= new Table();
  table.addColumn("id"); //This column stores a unique identifier for each record. We will just count up from 0 - so your first reading will be ID 0, your second will be ID 1, etc. 
  
  table.addColumn("year");
  table.addColumn("month");
  table.addColumn("day");
  table.addColumn("hour");
  table.addColumn("minute");
  table.addColumn("second");
  table.addColumn("Load cell"); //the following are dummy columns for each data value. Add as many columns as you have data values. Customize the names as needed. Make sure they are in the same order as the order that Arduino is sending them!
 }

 
void serialEvent(Serial myPort){
  val = myPort.readStringUntil('\n'); //The newline separator separates each Arduino loop. We will parse the data by each newline separator. 
  if (val!= null) { //We have a reading! Record it.
    val = trim(val); //gets rid of any whitespace or Unicode nonbreakable space
    println(val); //Optional, useful for debugging. If you see this, you know data is being sent. Delete if you like. 
    float sensorVals[] = float(split(val, ',')); //parses the packet from Arduino and places the values into the sensorVals array. I am assuming floats. Change the data type to match the datatype coming from Arduino. 
   
    TableRow newRow = dataTable.addRow(); //add a row for this new reading
    newRow.setInt("id", table.lastRowIndex()); //record a unique identifier (the row's index)
    
    //record time stamp
    newRow.setInt("year", year());
    newRow.setInt("month", month());
    newRow.setInt("day", day());
    newRow.setInt("hour", hour());
    newRow.setInt("minute", minute());
    newRow.setInt("second", second());
    
    //record sensor information. Customize the names so they match your sensor column names. 
    newRow.setFloat("sensor1", sensorVals[0]);
    //newRow.setFloat("sensor2", sensorVals[1]);
    
    readingCounter++; //optional, use if you'd like to write your file every numReadings reading cycles
    
    //saves the table as a csv in the same folder as the sketch every numReadings. 
    if (readingCounter % numReadings ==0) //The % is a modulus, a math operator that signifies remainder after division. The if statement checks if readingCounter is a multiple of numReadings (the remainder of readingCounter/numReadings is 0)
    {
      fileName = str(year()) + str(month()) + str(day()) + str(dataTable.lastRowIndex()); //this filename is of the form year+month+day+readingCounter
      saveTable(dataTable, fileName); //Woo! save it to your computer. It is ready for all your spreadsheet dreams. 
     }
   }
}
void draw() {
  if (mousePressed == true) 
  { //if we clicked in the window
   myPort.write('T'); //send a 1
   println("T");   
  } 
  else 
  { //otherwise
  myPort.write('0'); //send a 0
  }   
}

```

Best regards!

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [September 7, 2021, 3:34pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/3 "2021-09-07T15:34:51Z")

</div>

Hi

Try this in setup

table = new Table();

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [September 7, 2021, 3:37pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/4 "2021-09-07T15:37:36Z")

</div>

@MiguelSanches yes you are true

 ![Screenshot_2021-09-07-19-36-15-399](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8d2c4b7224c4ab4664e8e03fe378f4417942ec6e.jpeg)

---

<div class="post-metadata">

### Author: ![nyeas001](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nyeas001](https://discourse.processing.org/u/nyeas001)
#### Post date: [September 7, 2021, 3:47pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/5 "2021-09-07T15:47:19Z")

</div>

Thanks @MiguelSanches , it helped. The previous error message is gone, but I am getting a new error message.

```auto
Error, disabling serialEvent() for COM3 null

```

Could you please help with that?

---

<div class="post-metadata">

### Author: ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)
#### Post date: [September 7, 2021, 4:12pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/7 "2021-09-07T16:12:11Z")

</div>

Read this

> <https://stackoverflow.com/questions/26070466/arduino-and-processing-code-error-disabling-serialevent/28727842#28727842>

---

<div class="post-metadata">

### Author: ![nyeas001](https://avatars.discourse-cdn.com/v4/letter/n/bc8723/32.png) [@nyeas001](https://discourse.processing.org/u/nyeas001)
#### Post date: [September 7, 2021, 7:24pm UTC](https://discourse.processing.org/t/saving-data-in-processing/32136/9 "2021-09-07T19:24:57Z")

</div>

Thanks @jafal. That was helpful.
