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
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
}
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.
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?