Saving data to a text file using arduino and processing

24.9k views Asked by At

I want to save data received serially on arduino to a text file using processing. Right now my code is for processing is as shown below.

import processing.serial.*;
Serial myPort;
int val;
void setup()
{
myPort = new Serial (this, "COM3", 1200);

}   

void draw()
{
if ( myPort.available() > 0)
{
val = myPort.read();
print((char)val);
}
}

The arduino receives aaaaaaaaa and it's also received by processing like that. What do I do to save it into a text file? Can someone modify my code? I'm a bit new to processing.

Also, if my received text was : aaaaaaaaaa bbbbbbbbbb

then how can i save the new paragraph as a new paragraph in the text file?

All help appreciated. Thanks!

Follow up:

import processing.serial.*;
Serial mySerial;
//import java.text.*;
//import java.util.*;
PrintWriter output;  
String[] list;
void setup() {
size(720,700);
background(255);
mySerial = new Serial( this, "COM3", 1200 );
output = createWriter("test5.txt");
}
void draw() {
if (mySerial.available() > 0 ) {
     String value = mySerial.readString();
     if ( value != null ) {
        fill(50);
        text(value,10,10,700,700);
        output.println(value);
      saveStrings("test5.txt", value);
     }
  } 
}

//void keyPressed() {
//output.flush(); // Writes the remaining data to the file
//output.close(); // Finishes the file
//exit(); // Stops the program
//}

I tried this code for saving data into a text file and got an error that said "The method saveString(String, String[]) in the type PApplet is not applicable for the argument (String, String) "

1

There are 1 answers

0
Nico On BEST ANSWER

Before reading on, make sure that you are successfully reading the data from your Arduino into String value. You can confirm that by simply doing the following:

println(value);

The above line will print out the String to the command line of the PDE. If you see nothing, that means your Arduino is not feeding anything through. In that case you need to fix other things.

Also, to make sure the above works without any other things getting in the way, comment out the lines giving you problems or doing any file I/O.

If you have confirmed that you are receiving inputs from your Arduino then read on.

You are using the method saveStrings() incorrectly. According to the documentation here: http://www.processing.org/reference/saveStrings_.html you should convert your String input into an array and then feed it into the method.

Right now you have the following line:

String value = mySerial.readString();

Add the following after that:

String[] valueArray = split(value, ' ');

The above is directly from the documentation and splits the input String at every space and saves it in an array. After this just pass the array into the saveStrings() method and you should be good to go, like so:

saveStrings("myTextFile.txt", valueArray);

From the documentation, this "[w]rites an array of Strings to a file, one line per String".

The error message that you get:

"The method saveString(String, String[]) in the type PApplet is not applicable for the argument (String, String) "

very clearly tells you that you are feeding saveString() method two Strings (saveString(String, String)) whereas what it expects is a String and a String array (saveString(String, String[])).