Dans cet exemple simple, dérivé de l’exemple "vitrual color mixer" de Arduino, on envoie la valeur du potentiomètre, qui est un chiffre entre 0 et 1023, trop grand donc pour être stocké en un seul octet.
Code Arduino
const int pot = A0;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(analogRead(pot));
delay(50);
}Code Processing
import processing.serial.*;
float lacouleur = 0;
Serial myPort;
void setup() {
size(200, 200);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
}
void draw() {
// set the background color with the color values:
background(lacouleur);
fill(255);
rect(30, width-300, 20, lacouleur);
}
void serialEvent(Serial myPort) {
// lire une chaine jusqu'au retour chariot:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// supprimer les espace en trop:
inString = trim(inString);
float col = float(inString);
// convertir en un chiffre de 0 a 255:
lacouleur = col/4;
}
}