0

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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.