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;
}
}
}