Catégories
Liens
Il est très facile d'envoyer une valeur avec la communication série, mais il souvent utile d'envoyer un paquet de valeurs. Par exemple envoyer les valeurs de deux ou plus capteurs.
// 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() { // Update Data with linear values firstSensor += 1; secondSensor -= 1; if (secondSensor < 0) secondSensor = 255; if (firstSensor > 255) firstSensor = 0; // Send Data Serial.print(firstSensor); Serial.print(","); // séparateur // Avec println on ferme le paquet de valeur, car "ln" Serial.println(secondSensor); // Sampling rate delay(5); }
// Get Serial Data from an Arduino // Parse it with a separator caracter "," // import the Processing serial library import processing.serial.*; Serial myPort; // The serial port // Test values int v1 =0; int v2 = 255; int x; void setup() { size(640,480); // Open serial port //printArray(Serial.list()); myPort = new Serial(this, Serial.list()[0], 9600); // Read bytes into a buffer until you get a linefeed (ASCII 10): myPort.bufferUntil('\n'); //draw with smooth edges: //smooth(); background(255); } void draw() { // Draw circles fill(#ff0000); ellipse(x, v1, 5, 5); fill(#00ff00); ellipse(x, v2, 5, 5); // Update x position x++; // Refresh screen if (x > 600) { background(255); 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'); // if you got any bytes other than the linefeed: myString = trim(myString); // split the string at the commas // and convert the sections into integers: int values[] = int(split(myString, ',')); if (values.length > 0) { v1 = values[0]; v2 = values[1]; //println(v2); } }