I am trying to read in 100 sensor data entries from the Arduino via the (pseudo) serial connection using Processing. The Processing sketch I am using is as follows:
// import the serial library for Processing
import processing.serial.*;
// define a new port object
Serial port;
PrintWriter outputFile;
int i = 0;
// setup a port
void setup()
{
outputFile = createWriter("data.txt");
port = new Serial(this, "COM3", 9600);
port.bufferUntil('n');
}
void draw()
{
delay(100); //must be the same delay as used in the arduino sketch
// create a string to store the read-in values
String serialBuffer = port.readString();
if(serialBuffer != null)
{
i++;
outputFile.println(serialBuffer);
outputFile.flush();
}
if(i==99)
{
outputFile.close();
exit();
}
}
Unfortunately, I get less than 100 entries stored in my data.txt file usually, and some of the entries (about 3-5) are showing linebreaks within them. What am I doing wrong? The serial monitor of the Arduino IDE is NOT open!