ahm, so after i recommended to use the firmat ( upload firmata standard example unchanged to arduino )
and do all the coding in processing,
you do the exact opposite, not use firmata, and play serial sending.
OK
what is the problem with this code?
first time you try serial communication and you send 200 lines per second?
how does it look from arduino IDE monitor ( terminal?)
pls show the printout.
and in processing, after
myString = trim(myString);
pls do a print and show the result here.
i tested the whole thing here ( Raspberry Pi /usb/ Arduino Leonardo )
clean up / make real slow…
arduino code:
// Send serial data to Processing
// Data are separated by a ",", so you could send any number of data
int firstSensor = 0; // first analog sensor
int secondSensor = 255; // second analog sensor
void setup() {
Serial.begin(9600);
while (!Serial) { ; } // wait for serial port to connect. Needed for native USB port only
}
void loop() {
firstSensor += 1; // Update Data with linear values
if (firstSensor > 255) firstSensor = 0;
secondSensor -= 2;
if (secondSensor < 0) secondSensor = 255;
// Send Data
Serial.print(firstSensor);
Serial.print(","); // séparateur
Serial.println(secondSensor); // Avec println on ferme le paquet de valeur, car "ln"
delay(500); //5); // Sampling rate
}
/* see monitor:
127,1
128,255
129,253
130,251
131,249
132,247
133,245
134,243
135,241
136,239
137,237
138,235
139,233
*/
processing code:
// https://discourse.processing.org/t/use-the-arduino-processing-interface/9409/3
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
// Test values
int v1 = 0;
int v2 = 0;
int x;
void setup() {
size(640,480);
// Open serial port
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n'); // Read bytes into a buffer until you get a linefeed (ASCII 10):
background(255);
}
void draw() {
// Draw circles
fill(#ff0000);
ellipse(x, v1, 5, 5);
fill(#00ff00);
ellipse(x, v2, 5, 5);
x++; // Update x position
if (x > 600) {
background(255); // Refresh screen
x = 0;
}
}
// serialEvent method is run automatically by the Processing applet // whenever the buffer reaches the byte value set in the bufferUntil() method in the setup():
void serialEvent(Serial myPort) { // read the serial buffer:
String myString = myPort.readStringUntil('\n');
myString = trim(myString); // if you got any bytes other than the linefeed:
println(myString);
int values[] = int(split(myString, ',')); // split the string at the commas// and convert the sections into integers:
if (values.length > 0) {
v1 = values[0];
v2 = values[1];
}
}
