Poteniometer Volume Controller (Mapping the Volume on Mac)

Hi everyone, I have been trying to make a small media control hub and 1 of the features I wanted to make was volume control for my Mac. as I read online I saw people successfully achieve this and I mimicked versions of code to achieve similar results. Unfortunately for me I was unable to replicate them but I noticed one of them said that the audio was mapped for Windows.

To further explain my potentiometer uses about 50-60% to actually control the volume. the space above the max volume does nothing, and the space below starts inverting an adding my volume when I go below mute.

Here is version 1 and then Version 2 is below broken up by a line of dashes or "-"

My theory is the section where on both codes is said as " map(Aval, 0, 1023, 0, 55);" I think this section and the area around it might be causing my problem but adjusting these values hasn't helped me much, as far as I know its there to set up the amount of steps?

Im curious to hear your opinions and feedback and any help would be greatly appreciated.

Big thanks

//Version 1
#include <HID.h>                            
#include <HID-Project.h>                    
#include <HID-Settings.h>
#include <math.h>                           
#define REVERSED false                      

int Aval = 0;
int val = 0;
int volume = 0;
int potbuffer = 4;

void setup() {
  Consumer.begin();                        
  delay(1000);                              
  for(int a = 0; a < 52; a++) {
    Consumer.write(MEDIA_VOLUME_DOWN);      
  }
}

void loop() {
  if (analogRead(1) >= Aval+potbuffer || analogRead(1) <= Aval-potbuffer) { Aval = analogRead(1); }                                    
  val = map(Aval, 0, 1023, 0, 55);                                                                                                     
  val *= 2;                                                                                                                            
  if(REVERSED) {                                                                                                                      
    val = 110 - val;
  }

  while (volume != val) {                                                                                                              
    if (volume < val) { Consumer.write(MEDIA_VOLUME_UP); volume += 2; }                                                               
    if (volume == val) { break; }                                                                                                    
    if (volume > val) { Consumer.write(MEDIA_VOLUME_DOWN); volume -= 2; }                                                            
}

//-----------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------

//Version 2

#include  "HID-Project.h"

int pot_value;
int old_volume = 0;

void setup() {
  pinMode (A1, INPUT);

  Consumer.begin();

}

void loop() {
  pot_value = analogRead(A1);
  pot_value = map(pot_value, 1023, 0, 0, 50);

  if (pot_value != old_volume)  {
    if (pot_value > old_volume)  {
      Consumer.write (MEDIA_VOLUME_UP);
      old_volume = pot_value;
    }
    else if (pot_value < old_volume)  {
      Consumer.write (MEDIA_VOLUME_DOWN);
      old_volume = pot_value;
    }
  }

}

You should make two different code blocks.

This seems to invert pot_val or make it go negative.

Sorry, I'm a little confused about that. From what I can understand I should split them up into:

pot_value = map(pot_value, 0, 50);
pot_value = map(pot_value, 1023, 0,);

I'll look a little into the library I'm using and see what other information I can find but I'll definitely try my best, if you have any other tips then that would be great.
Thank you

This part of your code looks right...

If you want to invert this, you would invert the output min and max (55, 0):

val = map(Aval,  0, 1023, 55, 0);

In pseudo-code, it is:

val = map (Aval, inputMIN, inputMAX, outputMIN, outputMAX);

"... two different code blocks..." meant, put the sketch labeled "version 1" into one code block, and "version 2" into another code block.

1 Like

Thank you very much for the info on how the mapping works, ill try out some different sets of values to see what suits my mac best.

For the code I already have them set up in 2 different sketches I just combined them here for the sake of convenience but I appreciate the concern and help.

Ill need some time to try it out but ill inform you if I get it to work.
Big Thanks

Sorry for the late update but I have gotten it to work after playing around with the values primarily for the output to get everything set up and switched over to a different pin. I still have some dead zone once I reach 100% volume but its nothing I cant deal with.

I appreciate all your help and tips it helped me finish my little media hub project.

Thank you

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.