Michael
Michael

Reputation: 290

Storing Arduino data read in from serial connection using Processing

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!

Upvotes: 0

Views: 835

Answers (1)

kevinsa5
kevinsa5

Reputation: 3411

In your setup() function, I believe you mistyped the newline character:

port.bufferUntil('n');

Did you mean '\n'? Right now you're buffering until an 'n' comes along, which doesn't seem obvious. It seems that many of the tutorials pass it an int with the value of 10, which is the newline character in ASCII.

Upvotes: 1

Related Questions