Save Meassurement Data with processing to excel

Hi Guys,
I want to save meassurement datas with processing in an excel file.
To test my code I programmed the arduino to print the integer number 20 every 100msec to the serial monitor.

This is my processing code:

import processing.serial.*;
Serial myPort;

Table table;

int val; 
int numReadings = 10;  
int readingCounter = 0;

void setup() {
 String portName = Serial.list()[0]; 
 myPort = new Serial(this, portName, 9600);
  
  table = new Table();
  
  table.addColumn("id");
  table.addColumn("minute");
  table.addColumn("second");
  table.addColumn("wert");
  

}

void draw() {
  
  val = myPort.read(); 
  println(val); 
    
      TableRow newRow = table.addRow();
  newRow.setInt("id", table.lastRowIndex());
  newRow.setInt("minute", minute());
  newRow.setInt("second",second());
  newRow.setInt("wert", val);
  
  readingCounter++;
  
 if (readingCounter % numReadings ==0)
 {
   saveTable(table, "data/new8.csv");
 }
}

it should simply print:
number of table row, minute, second, 20

but it prints:

88,37,58,50
89,37,58,48
90,37,58,13
91,37,58,10
92,37,58,-1
93,37,58,-1
94,37,58,50
95,37,58,48
96,37,58,13
97,37,58,10
98,37,58,-1
99,37,58,-1 and so on…

why does it show 50 then 48 then 13 then 10 and then two times -1?
And how can I convert that to the original integer number 20?

I tried this:

 val = myPort.readString();
 newRow.setString("wert", val);

but then the value 20 is only in the last row of the excel file. In all other rows only row id, minute and seconds are printed. Like this:

82,8,58,
83,8,58,
84,8,58,
85,8,59,
86,8,59,
87,8,59,"20

dude, sorry but this is the Arduino Forum and not Processing.

You need to post it on the Processing Forum here: All Categories - Processing 2.x and 3.x Forum

Some of us here know a bit about Processing.

@Daniel1806, you have several problems with your processing code. First, you should be implementing the serialEvent() method. Serial IO has no place in draw().

Second, you should be using the bufferUntil() method of the Serial instance, so that the serialEvent() function is only called when the complete packet is ready to be read.

You must, of course, make sure that the Arduino IS sending whatever character you buffer until.
Third, you are reading a String from the serial port, and treating that as an int. That does not seem logical to me.

thanks for the fast response.
I tried the serial Event and buffer stuff allready.

This is the new version:

import processing.serial.*;
Serial myPort;

Table table;

int val; 
int numReadings = 10;  
int readingCounter = 0;

void setup() {
 String portName = Serial.list()[0]; 
 myPort = new Serial(this, portName, 9600);
  
  table = new Table();
  
  table.addColumn("id");
  table.addColumn("minute");
  table.addColumn("second");
  table.addColumn("wert");
  
  myPort.bufferUntil('\n');
}

void serialEvent(Serial myPort){
  
  val = myPort.read(); 
  println(val); 
    
      TableRow newRow = table.addRow();
  newRow.setInt("id", table.lastRowIndex());
  newRow.setInt("minute", minute());
  newRow.setInt("second",second());
  newRow.setInt("wert", val);
  
  readingCounter++;
  
 if (readingCounter % numReadings ==0)
 {
   saveTable(table, "data/new9.csv");
 }
}

now simply nothing happens. No file is created, and nothing is printed to monitor.
I included println(val); to the part: void serialEvent(Serial myPort)
--> nothing is printed out
--> so I think the programm is not running the void serialEvent(Serial myPort) part!!
Why is that?

Why is that?

Suppose I told you to dance when it rains. Now, suppose I am complaining that you are not dancing. What would be the MOST likely reason you are not dancing? (Well, after "I'm not going to dance in the rain!", anyway.)

Is your Arduino code sending a \n?

Is your Arduino code sending a \n?

well, I use Serial.println() so processing should read until the end of line, what I achieve with '\n' right?

this is my arduino test-code:

void setup(){
Serial.begin(9600); //This line tells the Serial port to begin communicating at 9600 bauds
}
 
//
void loop(){
//read each sensor value. We are assuming analog values. Customize as nessary to read all of your sensor values into the array. Remember to replace "sensor1Pin" and "sensor2Pin" with your actual pin names from above!
int sensor0 = 20;

Serial.println(sensor0);
delay(100);

}

Suppose I told you to dance when it rains. Now, suppose I am complaining that you are not dancing. What would be the MOST likely reason you are not dancing? (Well, after "I'm not going to dance in the rain!", anyway.)

you mean the void serialEvent(Serial myPort) part is not runing, because there's no data in the serial port?? Confused...

you mean the void serialEvent(Serial myPort) part is not runing, because there’s no data in the serial port?? Confused…

No. I meant that the most likely reason was that you were not sending a \n. But, it appears that you are.

So, the next most likely scenario is that the Arduino is not connected to the first port in the list.

Try printing the list:

  println("COM ports");
  println(Serial.list());
  println("=========");

Is the Arduino actually connected to the first port?

I printed the list and: yes it is the first (and the only one), otherwise it shouldn’t work with the void draw loop, neither. But it does…

I still don’t get it, why the integer number 20 is printed as -1, -1, 50, 48, 13, 10 and again -1, -1, 50 and so on…

@PaulS : I didn't mean it in a way to discourage Daniel1806 from posting here. But simply make sure the Processing community would also benefit and (perhaps help him quicker).

@Daniel1806: I can see that the value of 20 is being broken down per digit (or character) to its ASCII Decimal equivalent. So
so for 20:
ASCII (decimal) 50 = 2
ASCII (decimal) 48 = 0
ASCII (decimal) 13 = CR, Carriage Return
ASCII (decimal) 10 = NL, New Line or Line Feed

I am not sure where the -1 is coming from, I can only guess it's the (End and Start of the serial message). Serial messages are normally high which may explains the -1

On the Arduino Serial Monitor using print to write 20 would yield in 2 written first, followed by 0 (i.e. serially!). If we needed to write the 20 as a whole we'd use Serial.write(20) to write a string

Serial.print((char) value); is an alternative.

here's a thread on print vs write : http://forum.arduino.cc/index.php?topic=42603.0

I do appreciate that this isn't what you are looking for, but I thought I'd share on the hope it will help.

Once you have used the bufferUntil() method to assure that serialEvent() is called only when the appropriate end of packet marker has arrived, you need to use readStringUntil() in serialEvent() to read the whole STRING.

You can NOT just assign a String to an int. There are functions for converting a String to an int.