Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.