I threw together some code to send data from Arduino to Processing.
On the Processing side I converted the incoming String data to data type (String, int or float) and stored in array.
It may help as an example.
Arduino:
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
prevTime = millis();
}
void loop()
{
> Blockquote
currTime = millis();
diffTime = currTime - prevTime;
prevTime = currTime;
// send the value of analog input 0:
Serial.print(count++); Serial.print(' ');
Serial.print(millis()); Serial.print(' ');
Serial.print(diffTime); Serial.print(' ');
Serial.print(random(0, 255));
Serial.println();
delay(1);
}
Processing:
import processing.serial.*;
Serial myPort;
String vals;
String[] splitvals;
String[] d0 = new String[1000];
int[] d1 = new int[1000];
int[] d2 = new int[1000];
float[] d3 = new float[1000];
int i = 0;
void setup()
{
// List all the available serial ports:
printArray(Serial.list());
String portName = Serial.list()[2];
myPort = new Serial(this, portName, 9600);
myPort.clear();
delay(100);
myPort.clear();
}
void draw()
{
if (myPort.available() > 0)
{
vals = myPort.readStringUntil('\n');
println(vals);
}
// vals = ("one two three four");
if (vals != null && true) // Check for null and other condition "true"
{
splitvals = split(vals, ' ');
saveStrings("meineDatei.txt", splitvals);
printArray(splitvals);
d0[i] = splitvals[0];
d1[i] = int(splitvals[1]);
d2[i] = int (splitvals[2]);
d3[i] = float(splitvals[3]);
println(d0[i], d1[i], d2[i], d3[i]);
println("-----------------------------------------");
i++;
}
}
Output:
0] "COM4"
[1] "COM5"
[2] "COM6"
[3] "COM10"
[4] "COM11"
0 0 0 232
[0] "0"
[1] "0"
[2] "0"
[3] "232
"
0 0 0 232.0
-----------------------------------------
1 1 1 19
[0] "1"
[1] "1"
[2] "1"
[3] "19
"
1 1 1 19.0
-----------------------------------------
2 2 1 158
[0] "2"
[1] "2"
[2] "1"
[3] "158
"
2 2 1 158.0
-----------------------------------------
Output can be cleaned up (That quote did not belong there all alone like that!) by adding:
vals = trim(vals);
I will leave it as an exercise to put in the correct place in your code.
It will likely crash when i goes over 999!
Update:
It WILL crash when i goes over 999! Just tried it…
1000 17382 20 81
[0] "1000"
[1] "17382"
[2] "20"
[3] "81"
ArrayIndexOutOfBoundsException: 1000
