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 | ||
|
wiki:flossmanuals:un-capteur-une-application:accueil [2021/05/04 04:53] damien.muti |
wiki:flossmanuals:un-capteur-une-application:accueil [2021/05/04 05:29] (Version actuelle) damien.muti |
||
|---|---|---|---|
| Ligne 24: | Ligne 24: | ||
| 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. | 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. | ||
| - | ===== Plans et schémas de fonctionnement | + | ===== Programme Arduino - Potentiomètre |
| + | On utilise un [[wiki: | ||
| + | Le schéma de câblage est le suivant : | ||
| - | ===== Programmes ===== | ||
| - | ==== Références : Contrôle d’accès par badge RFID avec Arduino ==== | ||
| - | Le programme permettant de détecter l' | ||
| + | {{ : | ||
| - | ===== Réalisation | + | Le programme Arduino est le suivant : {{ : |
| - | vidéos, photos du making | + | |
| + | < | ||
| + | int firstSensor | ||
| + | int inByte | ||
| + | |||
| + | 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(); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // if we get a valid byte, read analog ins: | ||
| + | if (Serial.available() > 0) { | ||
| + | // get incoming byte: | ||
| + | inByte | ||
| + | |||
| + | // lecture de la valeur du potentiometre branché sur A0 | ||
| + | //et conversion de la valeur en un octet | ||
| + | firstSensor | ||
| + | |||
| + | // send sensor values: | ||
| + | Serial.write(firstSensor); | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | void establishContact() { | ||
| + | while (Serial.available() <= 0) { | ||
| + | Serial.print(' | ||
| + | delay(300); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | ===== Programme Arduino - Ultrasonic ===== | ||
| + | |||
| + | On utilise un [[wiki: | ||
| + | Le schéma de câblage est le suivant : | ||
| + | {{ : | ||
| + | |||
| + | Le capteur Ultrason est branché sur le " | ||
| + | |||
| + | Le programme Arduino est le suivant : {{ : | ||
| + | |||
| + | < | ||
| + | #include " | ||
| + | Ultrasonic ultrasonic(7); | ||
| + | long distance=0; // variable qui stoke la valeur de la distance | ||
| + | int firstSensor | ||
| + | int inByte | ||
| + | |||
| + | 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(); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // if we get a valid byte, read analog ins: | ||
| + | if (Serial.available() > 0) { | ||
| + | // get incoming byte: | ||
| + | inByte | ||
| + | |||
| + | // lecture de la distance | ||
| + | distance | ||
| + | // conversion de la valeur en un octet | ||
| + | firstSensor | ||
| + | |||
| + | // send sensor values: | ||
| + | Serial.write(firstSensor); | ||
| + | |||
| + | } | ||
| + | } | ||
| + | |||
| + | void establishContact() { | ||
| + | while (Serial.available() <= 0) { | ||
| + | Serial.print(' | ||
| + | delay(300); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== Programme Processing ===== | ||
| + | |||
| + | **Quel que soit le capteur utilisé**, le programme Processing est le suivant : {{ : | ||
| + | |||
| + | < | ||
| + | import processing.serial.*; | ||
| + | |||
| + | int bgcolor; | ||
| + | int fgcolor; | ||
| + | Serial myPort; | ||
| + | int serialInArray; | ||
| + | int serialCount = 0; // A count of how many bytes we receive | ||
| + | int xpos, ypos; | ||
| + | boolean firstContact = false; | ||
| + | |||
| + | void setup() { | ||
| + | size(256, 256); // Stage size | ||
| + | noStroke(); | ||
| + | |||
| + | // 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, | ||
| + | |||
| + | // espace HSB des couleurs | ||
| + | colorMode(HSB); | ||
| + | } | ||
| + | |||
| + | void draw() { | ||
| + | background(bgcolor); | ||
| + | fill(fgcolor, | ||
| + | // Draw the shape | ||
| + | circle(xpos, | ||
| + | } | ||
| + | |||
| + | 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' | ||
| + | // had first contact from the microcontroller. | ||
| + | // Otherwise, add the incoming byte to the array: | ||
| + | if (firstContact == false) { | ||
| + | if (inByte == ' | ||
| + | myPort.clear(); | ||
| + | firstContact = true; // you've had first contact from the microcontroller | ||
| + | myPort.write(' | ||
| + | } | ||
| + | } else { | ||
| + | // Add the latest byte from the serial port to array: | ||
| + | fgcolor = inByte; // remplir la donnée " | ||
| + | // print the values (for debugging purposes only): | ||
| + | println(" | ||
| + | |||
| + | // Send a capital A to request new sensor readings: | ||
| + | myPort.write(' | ||
| + | // Reset serialCount: | ||
| + | } | ||
| + | } | ||
| + | </ | ||