We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everybody
I'm currently trying to send some data from Arduino to Processing, so I can save it in a file. To be more specific, there are two variables (frequency of a blinking LED and respective LED number) which should be saved in tabular form.
Hereby the following Processing code is used:
import processing.data.Table;
import processing.serial.*;
Serial myPort;
Table dataTable; //table to store my values.
int lf = 10;
String Arduinostring = null;
float values;
String fileName;
void setup()
{
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600); //the port I'm using to listen to the serial port
dataTable = new Table();
dataTable.addColumn("id");
//columns for values
dataTable.addColumn("Data");
}
void draw() {
while (myPort.available() > 0) {
Arduinostring = myPort.readStringUntil(lf);
if (Arduinostring != null) {
print(Arduinostring); // Prints String
values=float(Arduinostring); // Converts and prints float
println(values);
}
TableRow newRow = dataTable.addRow(); //add a row for this new reading
newRow.setInt("id", dataTable.lastRowIndex());//record a unique identifier (the row's index)
//record information.
newRow.setFloat("Data", values);
}
saveTable(dataTable, "data/new.csv"); // save it to computer.
}
My intention here is to get my data into the table (and into my file). I managed to receive the required data (frequency, LED number), but this data isn't saved in my file. The mentioned file is created by the way and at least has the right ID for each Row.
What I simply do not understand is that he can't save it though the received data should be converted to float with the command num=float(myString), shouldn't it?
Many thanks in advance for any help!
Answers
To clarify, the file is created and contains data? From what I understand from your post, the file contains the correct tow number, maybe a comma separator and nothing more?
Points to keep in mind: You do not need to specify "data/new.csv" but "new.csv". As described in the reference, when you save the table, it will be stored in the "data" folder of your sketch folder. You have to make sure that folder exist. You are saving the table on draw without any control. Although this could work you will need to remember it is being saved many times per second which is inefficient. I personally will save the table under certain action like mousePressed or you can do the following:
If your program is running at 30 fps then it will save the table every two seconds or after 60 executing the draw function 60 times.
Related to your last question, float num=float(myString) should work. as described in the reference, myString has to be a valid number format, otherwise it will return NaN, not a number.
I hope this helps. Could you print about 10 lines from your println code? Something I notice is that you are saving the row number as your ID in your data table. Although I know you did this in purpose, it makes me wonder why you don't save the actual led ID? If it doesn't work, then the problem could fall in the parsing of the incoming data from the arduino.
Kf
https://www.Reddit.com/r/processing/comments/51vdxt/trouble_with_arduino_serial_communication/
https://forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1
First of all, I would like to thank you guys for your comments! GoToLoop, the links you posted were most helpful!
In the interest of all, who have the same problem ,I'm posting the Processing code that now works for me:
It is much different from the one I used before, because it's definetly easier to just save the string instead of converting the data and creating a table.
Many thanks again for your quick and of course helpful responses!
Thank you for sharing your final code!