Savid sensor data from arduino to processing in .csv file

Hello,
I've got problems uploading serial data from arduino to processing and saving it in a .csv file
Not always does the right value goes to the right column cell

My arduino script goes as follow:

//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures

/*

  • setup() - this function runs once when you turn your Arduino on
  • We initialize the serial connection with the computer
    */

float val = 0;
void setup()
{
Serial.begin(9600);
}

void loop() // run over and over again
{

val = analogRead(sensorPin);

Serial.println(val);// print out the voltage

delay(20); //waiting a second

}

My processing script goes as follow :

import processing.serial.*;
Serial mySerial;
PrintWriter output;
void setup() {
mySerial = new Serial( this, Serial.list()[3], 9600 );
output = createWriter( "databestandje.csv" );
}
void draw() {
if (mySerial.available() > 0 ) {
String value = mySerial.readString();
if ( value != null ) {
output.println( value );
}
}
}

void keyPressed() {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}

The output of my .csv file goes as follow

Can someone explain why for example at row 11 and 12 gives 55 and 5.00 while it should give 555.00

How about structuring your input by using

int lf = 10;    // Linefeed in ASCII
...
mySerial.readStringUntil(lf);

instead of grabbing the whole buffer?

Why do you run the communication with 9600 baud?

Arduino (did not put out V but the raw reading) 115200 baud 10 samples/second

//TMP36 Pin Variables
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures

/*
 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
 */
float val = 0;
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  val = analogRead(sensorPin)*5.0/1024;
  Serial.println(val);// print out the voltage
  delay(100);
}

Processing (parsing slightly changed) 115200 baud

import processing.serial.*;
Serial mySerial;
PrintWriter output;
int lf = 10;    // Linefeed in ASCII
void setup() {
  mySerial = new Serial( this, Serial.list()[3], 115200 );
  output = createWriter( "databestandje.csv" );
}
void draw() {
  if (mySerial.available() > 0) {
    String value = mySerial.readStringUntil(lf);
    if ( value != null ) {
      if (value.length() > 2) {
        output.print( value );
      }
    }
  }
}

void keyPressed() {
  output.flush();  // Writes the remaining data to the file
  output.close();  // Finishes the file
  exit();  // Stops the program
}

thanks

void draw() {
    if (mySerial.available() > 0 ) {
         String value = mySerial.readString();
         if ( value != null ) {
              output.println( value );
         }
    }
}

DO NOT DO SERIAL INPUT IN DRAW!

Implement the serialEvent() method and do your serial input there.

PaulS:
DO NOT DO SERIAL INPUT IN DRAW!

Where in the reference is that paradigm hidden?
Please give me a link, I must have overlooked that.

Especially in a program that does not 'draw' anything, it really does not hurt.

....

Especially in a program that does not 'draw' anything, it really does not hurt.

Unlike loop(), draw() is not called over and over AS FAST AS POSSIBLE.

So, doing serial input in draw() is NOT a good idea.

The standard rate draw is called is 60.
If that is too slow, it is very easy to change it to a higher rate.
A little jumpy numbers, but in this trivial case always above 2500/second.

void setup () {
  frameRate(2500);
}
void draw() {
  if ((frameCount % 100) == 0) {
    println(frameRate);
  }
}
3077.0906
2718.8496
2554.2317
2640.7302
2632.8862
2924.133
2704.001
3653.8347
3059.7056
2655.6528
3285.6418
2958.2666
2578.484
2710.7888
2674.1074
2707.0503
2865.8848
2896.6765
2909.2925
2703.6401
3008.047
2809.1423
3000.7756
2774.1836
2740.6882
3171.4553
3871.6885
2951.7837
3226.798
2728.3484
3207.1443
2813.1392
2942.9438
3082.6218

There are events that need to be dealt with when serial data arrives, like the end of packet marker arriving. Why diddle with frame rate, etc., as opposed to just doing serial handling right?

Does this version satisfy you?

import processing.serial.*;
Serial mySerial;
PrintWriter output;
int lf = 10;    // Linefeed in ASCII
void setup() {
  mySerial = new Serial( this, Serial.list()[3], 115200 );
  output = createWriter( "databestandje.csv" );
  mySerial.bufferUntil(lf);
}
void draw() {}
void serialEvent(Serial p) {
  String value = p.readString();
  if ( value != null ) {
    if (value.length() > 2) {
      output.print( value );
    }
  }
}
void keyPressed() {
  output.flush();  // Writes the remaining data to the file
  output.close();  // Finishes the file
  exit();  // Stops the program
}

Does this version satisfy you?

It satisfies me. Whether it does what the OP expects, or not, remains to be seen.