Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


logiciels:serial-processing:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
logiciels:serial-processing:accueil [2016/05/25 10:15]
resonance [Code Arduino]
logiciels:serial-processing:accueil [2018/01/22 11:19] (Version actuelle)
resonance ancienne révision (2016/05/25 10:28) restaurée
Ligne 1: Ligne 1:
 ====== Communication série Arduino/Processing ====== ====== Communication série Arduino/Processing ======
  
 +
 +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.
  
 ===== Code Arduino ===== ===== Code Arduino =====
Ligne 27: Ligne 29:
     // Send Data     // Send Data
     Serial.print(firstSensor);     Serial.print(firstSensor);
-    Serial.print(","); +    Serial.print(","); // séparateur 
-    Serial.println(secondSensor);+    // Avec println on ferme le paquet de valeur, car "ln" 
 +    Serial.println(secondSensor); 
          
     // Sampling rate     // Sampling rate
     delay(5);     delay(5);
 +}
 +</code>
 +
 +
 +===== Code Processing =====
 +
 +<code java>
 +// 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);
 +  }
 } }
 </code> </code>
/home/resonancg/www/wiki/data/attic/logiciels/serial-processing/accueil.1464164140.txt.gz · Dernière modification: 2016/05/25 10:15 de resonance