Hello, I need to code a program that retrieves a value from the Arduino card and plays a sound based on that value.
The value varies according to the position of a potentiometer: an Arduino program transforms the voltage across the potentiometer into 3 digital values: 1, 2 and 3.
The Processing program must play sound 1 when the recovered value is 1, play sound 2 when the recovered value is 2 and play sound 3 when the value is 3.
I must point out that when you switch from sound 1 to sound 2 (or from sound 2 to 3, etc.), sound 1 is not stopped but only muted. Once the sound has been played in its entirety, it must be automatically restarted.
For the moment I have written this program, but it doesn’t work.
Could someone please help me?
I am French so sorry for my English.
My code :
import processing.serial.*;
import ddf.minim.*; //Import de la bibliothèque "minim" pour lecture des sons
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
Serial port; //L'objet "port" qui gère la lcture des sons
Minim minim; //L'objet "minim" qui gère la lcture des sons
AudioPlayer radio1; //Objet contenant les données audio de la premiere radio
AudioPlayer radio2; //Objet contenant les données audio de la deuxieme radio
AudioPlayer radio3; //Objet contenant les données audio de la troisieme radio
int value=0; //valeur lue sur le port série, envoyé par la carte Arduino
void setup() {
size(800, 600); //Crée une fenêtre vierge de 800*600 pixels
port = new Serial(this, "COM7", 9600); // !!!!!!!!! A CHANGER !!!!!!!!!!!!!
port.bufferUntil('\n'); //Attendre arrivée d'un saut de ligne pour générer évènement série
minim = new Minim(this);
radio1 = minim.loadFile("radio1.mp3");
radio2 = minim.loadFile("radio2.mp3");
radio3 = minim.loadFile("radio3.mp3");
}
void draw() {
background(0,60,60); //Couleur de l'arrière plan en RVB
radio();
delay(100);
}
void radio(){
println(value);
if (!radio1.isPlaying() && value==1){
radio1.rewind();
radio1.play();
}
if (!radio2.isPlaying() && value==2){
radio1.rewind();
radio1.play();
}
if (!radio3.isPlaying() && value==3){
radio1.rewind();
radio1.play();
}
}
void serialEvent(Serial port){
String serialStr = port.readStringUntil('\n');
serialStr=trim(serialStr);
int values[]=int(split(serialStr,','));
if(values.length==2){
value=values[1];
}
}