====== Un Capteur - Une application : Dialogue simple entre Arduino et Processing ====== * **Porteur(s) du projet** : Damien MUTI (Prof. de Numérique) * **Date** : 05/2021 * **Contexte** : Cours * **Fichiers** : * **Liens** : * **Capteurs/Actionneurs** : * Capteur de distance ultrason Grove * Potentiomètre * Processing ---- ===== Intentions : explication du projet et objectifs ===== L'objectif de cet article est de montrer sur un exemple simple comment dialoguer entre la carte Arduino et Processing. L'objectif est de mesurer la valeur lue sur UN capteur et de l'envoyer vers le programme Processing comme un paramètre pouvant faire varier la forme ou la couleur d'un graphisme. Les programmes proposés sont des formes simplifiés des exemples **[[https://www.arduino.cc/en/pmwiki.php?n=Tutorial/SerialCallResponse|SerialCallResponse]]** de Processing et Arduino. Le schéma suivant résume la situation : {{ :wiki:flossmanuals:un-capteur-une-application:un_capteur_une_application.png?800 |}} Nous proposons ici de mesurer une distance à l'aide d'un capteur de distance ultrason et de faire varier la couleur d'une forme sous Processing. ===== Programme Arduino - Potentiomètre ===== On utilise un [[wiki:tutoriels:arduino-capteurs:arduino-capteurs#potentiometre|potentiomètre de 10kΩ]]. Le schéma de câblage est le suivant : {{ :wiki:flossmanuals:un-capteur-une-application:potentiometre_a0_bb.png?400 |}} Le programme Arduino est le suivant : {{ :wiki:flossmanuals:un-capteur-une-application:un_potentiometre_une_application_arduino.zip |}} int firstSensor = 0; // first analog sensor int inByte = 0; // incoming serial byte void setup() { // start serial port at 9600 bps: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } establishContact(); // send a byte to establish contact until receiver responds } void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); // lecture de la valeur du potentiometre branché sur A0 //et conversion de la valeur en un octet firstSensor = analogRead(A0)/4; // send sensor values: Serial.write(firstSensor); } } void establishContact() { while (Serial.available() <= 0) { Serial.print('A'); // send a capital A delay(300); } } ===== Programme Arduino - Ultrasonic ===== On utilise un [[wiki:tutoriels:arduino-capteurs:arduino-capteurs#capteur_de_distance_ultrason_grove|capteur de distance Ultrason Ultrasonic en Grove]]. Le schéma de câblage est le suivant : {{ :wiki:tutoriels:arduino-capteurs:capteur_dist_ultrason_arduino_connection_grove.jpg?400 |}} Le capteur Ultrason est branché sur le "slot" D7. Le programme Arduino est le suivant : {{ :wiki:flossmanuals:un-capteur-une-application:un_capteur_une_application_arduino.zip |}} #include "Ultrasonic.h" Ultrasonic ultrasonic(7); long distance=0; // variable qui stoke la valeur de la distance int firstSensor = 0; // first analog sensor int inByte = 0; // incoming serial byte void setup() { // start serial port at 9600 bps: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } establishContact(); // send a byte to establish contact until receiver responds } void loop() { // if we get a valid byte, read analog ins: if (Serial.available() > 0) { // get incoming byte: inByte = Serial.read(); // lecture de la distance distance = ultrasonic.MeasureInCentimeters(); // conversion de la valeur en un octet firstSensor = map(distance,0,400,0,255); // send sensor values: Serial.write(firstSensor); } } void establishContact() { while (Serial.available() <= 0) { Serial.print('A'); // send a capital A delay(300); } } ===== Programme Processing ===== **Quel que soit le capteur utilisé**, le programme Processing est le suivant : {{ :wiki:flossmanuals:un-capteur-une-application:un_capteur_une_application_processing.zip |}}. On modifie la couleur d'un cercle positionné au centre de la fenêtre d'affichage. import processing.serial.*; int bgcolor; // Background color int fgcolor; // Fill color Serial myPort; // The serial port int serialInArray; // Where we'll put what we receive int serialCount = 0; // A count of how many bytes we receive int xpos, ypos; // Starting position of the ball boolean firstContact = false; // Whether we've heard from the microcontroller void setup() { size(256, 256); // Stage size noStroke(); // No border on the next thing drawn // Set the starting position of the ball (middle of the stage) xpos = width/2; ypos = height/2; // Print a list of the serial ports, for debugging purposes: printArray(Serial.list()); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); // espace HSB des couleurs colorMode(HSB); } void draw() { background(bgcolor); fill(fgcolor,255,255); // Draw the shape circle(xpos, ypos, 50); } void serialEvent(Serial myPort) { // read a byte from the serial port: int inByte = myPort.read(); // if this is the first byte received, and it's an A, // clear the serial buffer and note that you've // had first contact from the microcontroller. // Otherwise, add the incoming byte to the array: if (firstContact == false) { if (inByte == 'A') { myPort.clear(); // clear the serial port buffer firstContact = true; // you've had first contact from the microcontroller myPort.write('A'); // ask for more } } else { // Add the latest byte from the serial port to array: fgcolor = inByte; // remplir la donnée "utile" // print the values (for debugging purposes only): println("valeur lue sur le port Série = " + fgcolor); // Send a capital A to request new sensor readings: myPort.write('A'); // Reset serialCount: } }