Saving data in Processing

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

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?

Hi @nyeas001

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

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

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!

2 Likes

Hi

Try this in setup

table = new Table();

@MiguelSanches yes you are true

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

Error, disabling serialEvent() for COM3 null

Could you please help with that?

Read this

Thanks @jafal. That was helpful.