I am new to Processing and I dont know much of JAVA.
I used the following code from one of the post to get data in Processing from Arduino Uno which has accelerometer and gyroscope sensors plugged into it to get a text file. Basically I am trying to get a text file so that I can import it into matlab and further work on it. I can see the data coming in Processing, but I dont know where to look for the text file i.e. Data.txt
and also the data is continuously coming in from arduino to processing.
How do I get a text file of the data and use it in matlab?
//import serial library
import processing.serial.*;
//initialize Print Writer Object
PrintWriter OUTPUT;
// array object for processes sensor values
int sensorval[];
//variable for raw sensor values
int rawsensorval = 0;
//initialize serial port object
Serial port;
void setup() {
size(500,500);
background(0);
//list all serial values
println(Serial.list());
//new serial object
port = new Serial(this, Serial.list()[0],9600);
port.bufferUntil('\n');
//initialize text file to write to
OUTPUT = createWriter("Data.txt");
}
void draw(){
//write serial values to text file
convert();
}
void keypresses(){
//press "e" key to finish and export text file
if(key == 'e'){
exportText();
}
}
void serialEvent(Serial port) {
//println("Serial Event Begun");
//read serial buffer until you hit a new line character
String myString = port.readStringUntil('\n');
//println("String Declared and port read");
//check to make sure string is not = null
if (myString != null) {
//printing out the raw values incoming
println(myString);
//remove whitespace
myString = trim(myString);
/*sensorVal[] = int(split(myString, ',')); //split the string by commas and cast as ints into int array
for (int i = 0; i < sensorVal.length; i++) { //print out the values with their sensor names
print("SensorVal "+i+": "+sensorVal[i]+"\t");
*/
//convert string to float value
rawsensorval = int(myString);
}
//println();
}
//cast raw serial values into an array
//void processRawSerial(){
// sensorval = float(split(rawsensorval, ',')); //split the string by commas and cast as ints into int array
//}
//write values to text file
void convert(){
OUTPUT.println(sensorval);
OUTPUT.flush();
//println("Starting write to text");
}
//finish writing to text file
void exportText(){
OUTPUT.close();
super.stop();
println("data exported");
}
Any help would be much appreciated and I would be grateful.