Hi everyone,
I would like to control the volume of my audio file or my video with a classic analog sensor. So I founded these sketchs to link Arduino and Processing and I modified it to map my volume but I don’t understand why it doesn’t work. Anyone can help me ?
Sorry for my english, I’m French !`
Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
if(sensorValue > 1000) {
Serial.println("T");
// send the letter T (for Trigger) once the sensor value is bigger than 1000
}
delay(1);
}
Processing:
import processing.serial.;
import ddf.minim.;
import ddf.minim.effects.;
import ddf.minim.ugens.;
Minim minim;
AudioOutput output;
FilePlayer groove;
LowPassFS lpf;
int lf = 10; // Linefeed in ASCII
String myString = null;
Serial myPort; // The serial port
int sensorValue = 0;
void setup() {
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[2], 9600);
myPort.clear();
// Throw out the first reading, in case we started reading
// in the middle of a string from the sender.
myString = myPort.readStringUntil(lf);
myString = null;
// we pass this to Minim so that it can load files from the data directory
minim = new Minim(this);
// loadFile will look in all the same places as loadImage does.
// this means you can find files that are in the data folder and the
// sketch folder. you can also pass an absolute path, or a URL.
// Change the name of the audio file here and add it by clicking on “Sketch —> Import File”
output = minim.getLineOut();
groove = new FilePlayer( minim.loadFileStream(“groove.mp3”) );
// make a low pass filter with a cutoff frequency of 100 Hz
// the second argument is the sample rate of the audio that will be filtered
// it is required to correctly compute values used by the filter
lpf = new LowPassFS(100, output.sampleRate());
groove.patch( lpf ).patch( output );
groove.loop();
}
void draw() {
// check if there is something new on the serial port
while (myPort.available() > 0) {
// store the data in myString
myString = myPort.readStringUntil(lf);
// check if we really have something
if (myString != null) {
myString = myString.trim(); // let’s remove whitespace characters
// if we have at least one character…
if(myString.length() > 0) {
println(myString); // print out the data we just received
// if we received a number (e.g. 123) store it in sensorValue, we sill use this to change the volume.
try {
sensorValue = Integer.parseInt(myString);
for ( int sensorValue = 0; sensorValue < output.bufferSize() - 1; i++ ){
float x1 = map(sensorValue, 0, output.bufferSize(), 0, 10);
float x2 = map(sensorValue+1, 0, output.bufferSize(), 0, 10);
}
} catch(Exception e){}
if(myString.equals("T")){
if(
}
}
}
}
}
}