mercredi 19 mai 2010

Un entretient avec GODARD

dimanche 16 mai 2010

arduino vers processing "serial " "minim" "library"

Voici un petit programme pour déclencher un son en Fonction d'une valeur généré par ARDUINO.
Pour une explication voir le lien YOUTUBE ICI (en cours).

Le programme à mettre dans l'arduino:
---------------------------------------------------------------------------------------

// Sweep
// by BARRAGAN

#include
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int analogInput = 3;
int led = 13;
int analogValue = 0;

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(led, OUTPUT);
pinMode(analogInput, INPUT);
Serial.begin(9600);
}
void loop()
{
// pour voir si le montage fonctionne

for(pos = 0; pos <>
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if (pos <>
Serial.println(pos); //variable pos = 0 - 180 ne pas utiliser print mais println
Serial.print("\t");
delay(100);
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
if (pos <>
Serial.println(pos); //variable pos = 0 - 180
Serial.print("\t");
delay(100);
}
}
--------------------------------------------------------------------------------------


et le programme à mettre dans processing:


--------------------------------------------------------------------------------------

// reception de valeur par le port serie arduino 0 à 1023
// processing divise par 4 pour conversion en couleur donc valeur de 0 à 101
// test de la valeur si inferieure à 61 lecture du son
// au dessus pas de son
import processing.serial.*;

Serial port;
String buff1 = "";
String buff2 = "";
int NEWLINE = 10;
int oldval=0;
int farve=255;
int mode=0;
int val=0;
// Store the last 64 values received so we can graph them.
int[] values = new int[64];
// declaration du lecteur
import ddf.minim.*;
Minim minim;
AudioPlayer player;

void setup()
{
size(512, 512);
strokeWeight(3);
println("Available serial ports:");
println(Serial.list());
// Uses the first port in this list (number 0). Change this to
// select the port corresponding to your Arduino board. The last
// parameter (e.g. 9600) is the speed of the communication. It
// has to correspond to the value passed to Serial.begin() in your
// Arduino sketch.
port = new Serial(this, Serial.list()[1], 9600);
// If you know the name of the port used by the Arduino board, you
// can specify it directly like this.
//port = new Serial(this, "COM1", 9600);
// chargement de la musique dansla buffer
minim = new Minim(this);
player = minim.loadFile("Singapore.mp3", 2048);
// test de lecture au demarrage
// player.play();
}

void draw()
{
//change la couleur du fond en fonction de la valeur
background(oldval);
stroke(farve);
// Graph the stored values by drawing a lines between them.
for (int i = 0; i <>
line(i * 8, height - values[i]/100, (i + 1) * 8, height - values[i +1]/100);
// affiche la valeur reçu dans la Zone ecran
text("received: " + oldval, 10,50);
while (port.available() > 0)
serialEvent(port.read());
println(oldval);
int oldval2=constrain(oldval,0,500);
farve=(int)map(oldval2, 0,500,0,255);
//test de la valeur si inferieure à 61 pour lancer le lecteur
// possible de changer la valeur pour une autre 0, 10,90,180,...
if ( oldval < (90)) {
player.play() ;
println (" valeur inferieure à 90") ;
}
// si la valeur n'est pas inferieure stop le son
else{player.pause();
}
}

void serialEvent(int serial)
{
//
if (serial != NEWLINE) {
if(serial!= TAB){

buff1 += char(serial);
}
// Store all the characters on the line.
} else {
// The end of each line is marked by two characters, a carriage
// return and a newline. We're here because we've gotten a newline,
// but we still need to strip off the carriage return.
buff1 = buff1.substring(0, buff1.length()-1);
// Parse the String into an integer. We divide by 4 because
// analog inputs go from 0 to 1023 while colors in Processing
// only go from 0 to 255.

val = (Integer.parseInt(buff1)+oldval)/2;

//println("test");
// Clear the value of "buff"
buff1 = "";
// Shift over the existing values to make room for the new one.
for (int i = 0; i <>
values[i] = values[i + 1];
// Add the received value to the array.
values[63] = val;
oldval=val;
mode=0;
}
}
void stop()
{
// always close Minim audio classes when you are done with them
player.close();
minim.stop();
super.stop();}
--------------------------------------------------------------------------------------