Hi Forum
Im very new to processing and am trying to help someone out with a project.
this is based on code that i found and have tried to change to fit the needs for the project.
the idea is to use a proximity sensor to start and stop audio playback, the arduino sends “play” and “stop”
and processing seems to receive that and the audio starts to play when it receives the “play” but then nothing happens. it does not pause and rewind as i hoped for and it does not start again when triggered.
here is the code:
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
int lf = 10; // Linefeed in ASCII
String myString = null;
Serial myPort; // The serial port
int sensorValue = 0;
void setup() {
// List all the available serial ports
printArray(Serial.list());
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[1], 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"
player = minim.loadFile("What.mp3");
}
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 background color.
try {
sensorValue = Integer.parseInt(myString);
// As the range of an analog sensor is between 0 and 1023, we need to
// convert it in order to use it for the background color brightness
int brightness = (int)map(sensorValue, 0, 1023, 0, 255);
background(brightness);
} catch(Exception e){}
if(myString.equals("play")){
if(player.isPlaying() == false){
player.play();
delay(100);}
if(myString.equals("stop")){
if(player.isPlaying() == true){
player.pause();
delay(1000);
player.rewind();}
}
}
}
}
}
}
In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>
That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.
Extra info:
Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.
I have modified your draw() function and some of your code. Please keep in mind the code is not tested and it might have some bugs, either typos or actual functionality issues. I believe some of the changes below addresses your issues directly. Some other changes just offers overall improvement.
Kf
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player;
int lf = 10; // Linefeed in ASCII
String myString = null;
Serial myPort; // The serial port
int sensorValue = 0;
void setup() {
// List all the available serial ports
printArray(Serial.list());
// Open the port you are using at the rate you want:
myPort = new Serial(this, Serial.list()[1], 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"
player = minim.loadFile("What.mp3");
}
void draw() {
}
// Serial callback
void serialEvent(Serial aPort) {
// 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 (myString.equals("play")) {
if (player.isPlaying() == false) {
player.play();
}
} else if (myString.equals("stop")) {
if (player.isPlaying() == true) {
player.stop();
player.rewind();
}
} else {
// if we received a number (e.g. 123) store it in sensorValue, we sill use this to change the background color.
sensorValue = int(myString);
// As the range of an analog sensor is between 0 and 1023, we need to
// convert it in order to use it for the background color brightness
int brightness = sensorValue / 4; //Map from 1024 range down to a 256 range
background(brightness);
}
}
} // if (myString != null)
}