0
#include <HID-Project.h>                    //include HID_Project library
#include <HID-Settings.h>

#define REVERSED false                      //if your controller is reversed change it to true

int val = 0;
int previousval = 0;
int val2 = 0;

void setup() {
  Consumer.begin();                         //initialize computer connection
  delay(1000);                              //wait for computer to connect
  for(int a = 0; a < 52; a++) {
    Consumer.write(MEDIA_VOLUME_DOWN);      //set the volume to 0
    delay(2);
  }
}

void loop() {
  val = analogRead(A2);                      //read potentiometer value
  val = map(val, 0, 1023, 0, 101);          //map it to 102 steps
  if(REVERSED) {
    val = 101 - val;
  }
  if(abs(val - previousval) > 1) {          //check if potentiometer value has changed
    previousval = val;
    val /= 2;                               //divide it by 2 to get 51 steps
    while(val2 < val) {
      Consumer.write(MEDIA_VOLUME_UP);      //turn volume up to appropiate level
      val2++;
      delay(2);
    }
    while(val2 > val) {
      Consumer.write(MEDIA_VOLUME_DOWN);    //turn volume down to appropiate level
      val2--;
      delay(2);
    }
  }
  delay(301);                               //wait at least 300ms between changing volume levels
}                                           //if it will change faster Windows can sometimes 
                                            //increase or decrease volume by 10 steps at once
  • 1
    how is the code related to the question? – jsotola Mar 25 at 7:17
  • 1
    Sure it can. Just wire it correctly and install the right ArduinoISP sketch. – Majenko Mar 25 at 11:18