Ci-dessous, les différences entre deux révisions de la page.
| 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/ | ====== Communication série Arduino/ | ||
| + | |||
| + | Il est très facile d' | ||
| ===== Code Arduino ===== | ===== Code Arduino ===== | ||
| Ligne 27: | Ligne 29: | ||
| // Send Data | // Send Data | ||
| Serial.print(firstSensor); | Serial.print(firstSensor); | ||
| - | Serial.print("," | + | Serial.print("," |
| - | Serial.println(secondSensor); | + | // Avec println on ferme le paquet de valeur, car " |
| + | Serial.println(secondSensor); | ||
| | | ||
| // Sampling rate | // Sampling rate | ||
| delay(5); | delay(5); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== 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, | ||
| + | // Open serial port | ||
| + | // | ||
| + | myPort = new Serial(this, | ||
| + | |||
| + | // Read bytes into a buffer until you get a linefeed (ASCII 10): | ||
| + | myPort.bufferUntil(' | ||
| + | |||
| + | //draw with smooth edges: | ||
| + | //smooth(); | ||
| + | background(255); | ||
| + | } | ||
| + | |||
| + | void draw() { | ||
| + | |||
| + | // Draw circles | ||
| + | fill(# | ||
| + | ellipse(x, v1, 5, 5); | ||
| + | fill(# | ||
| + | ellipse(x, v2, 5, 5); | ||
| + | |||
| + | // Update x position | ||
| + | x++; | ||
| + | | ||
| + | // Refresh screen | ||
| + | if (x > 600) { | ||
| + | background(255); | ||
| + | x = 0; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // serialEvent | ||
| + | // 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(' | ||
| + | | ||
| + | // 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]; | ||
| + | // | ||
| + | } | ||
| } | } | ||
| </ | </ | ||