Ceci est une ancienne révision du document !
Intentions Chloé : Tableaux vivants avec capteur de distance
ARDUINO
#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); delay(200); }
}
void establishContact() {
while (Serial.available() <= 0) { Serial.print('A'); // send a capital A delay(300); }
}
PROCESSING
import processing.serial.*;
/ graphisme
int bgcolor; / Background color
int fgcolor; / Fill color
int xpos, ypos; / Starting position of the ball
int distance;
PImage[] tabImages;
int NImages = 3;
/port serie
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
boolean firstContact = false; / Whether we've heard from the microcontroller
void setup() {
size(600, 485); // 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()[9]; myPort = new Serial(this, portName, 9600);
// espace HSB des couleurs colorMode(HSB);
//creation tableau images tabImages=new PImage[NImages]; for (int i=0; i<NImages; i++) { tabImages[i]= loadImage("images"+i+".jpg"); }
}
void draw() {
background(bgcolor); // test distance if (distance >= 0 && distance < 50) { image(tabImages[0], 0, 0, width, height); } else if (distance >= 50 && distance < 100) { image(tabImages[1], 0, 0, width, height); } else if (distance >= 100 && distance < 150) { image(tabImages[2], 0, 0, width, height); } else { image(tabImages[0], 0, 0, width, height); //fill(0); //rect(0, 0, width, height); }
//fill(fgcolor, 255, 255); // Draw the shape //ellipse(xpos, ypos, 20, 20);
}
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: distance = (int) map(inByte, 0, 255, 0, 400); // remplir le donnée "utile" // print the values (for debugging purposes only): println("valeur lue sur le port Série = " + distance);
// Send a capital A to request new sensor readings: myPort.write('A'); // Reset serialCount: }
}