So when my game starts the music does also but id like to be able to control the volume of the music with a potentiometer.
I have sound.setGain(0); working in processing and I have the output of the potentiometer being sent over the serial port I just don’t know how to connect the two in Processing.
I have been looking online for tutorials or other forums but I can’t find any that help with this if anyone could point me in the right direction it would be great!
You want to get the Output from the Arduino over to Processing via Serial? Then you have to use the Serial library.
No i have the output already being sent over i just dont know how to use the output in processing to change sound.setGain() when using the potentiometer.
So for convenience, you have this as Code, right? :
String output; // you might have already converted that to a float
// and you have the method
//now you want that 0 to become the value of the output, right?...
//then just use
sound.setGain(float(output)); //or just output if you already converted it to float before
The arduino part provides a value within a range which depends in the resolution of your arduino input and the resolution of your source, the potentiometer in this case. Let’s say you have an 8 bit resolution meaning your potentiometer will take values from 0 to 2^8-1 or 0…255. I believe the
setGain() takes a value from 0 to 1, 1 to be the maximum volume. You need to map the range of your arduino to the range of your Sound object. For this, check the handy map function: https://processing.org/reference/map_.html
final float ARDUINO_MIN=0;
final float ARDUINO_MAX=255;
final float GAIN_MIN=0;
final float GAIN_MAX=1.0;
float newVolAmplitude = map(arduinoIn, ARDUINO_MIN,ARDUINO_MAX, GAIN_MIN,GAIN_MAX);