# Minim volume control using potentiometer

**URL:** <https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [December 8, 2019, 4:19pm UTC](https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253 "2019-12-08T16:19:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ardy2743](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Ardy2743](https://discourse.processing.org/u/Ardy2743)\
**Post date:** [December 8, 2019, 4:19pm UTC](https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253/1 "2019-12-08T16:19:50Z")

</div>

Hi peeps!

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!

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 8, 2019, 4:55pm UTC](https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253/2 "2019-12-08T16:55:38Z")

</div>

You want to get the Output from the Arduino over to Processing via Serial? Then you have to use the Serial library.

---

<div class="post-metadata">

**Author:** ![Ardy2743](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@Ardy2743](https://discourse.processing.org/u/Ardy2743)\
**Post date:** [December 8, 2019, 5:01pm UTC](https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253/3 "2019-12-08T17:01:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 8, 2019, 9:00pm UTC](https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253/4 "2019-12-08T21:00:51Z")

</div>

Uhm?  
So for convenience, you have this as Code, right? :

```auto
String output; // you might have already converted that to a float

// and you have the method

sound.setGain(0);

//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

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [December 8, 2019, 10:29pm UTC](https://discourse.processing.org/t/minim-volume-control-using-potentiometer/16253/5 "2019-12-08T22:29:58Z")

</div>

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](https://processing.org/reference/map_.html)

```auto
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);
sound.setGain(newVolAmplitude );

```

Kf
