Windows10 system volume controller

Hi,

Bit of a newbie here in need of advice. Updates seem to be progressing faster then I'm learning!

I made a frankenstein sketch for my Leonardo to control the system volume, largely with advice posted by Stefan Jones on his blog. This used a Remote class and mods to some core files.

All seemed to be working as intended with my Win7 setup... then I updated to Win10. My Leonardo no longer mounts properly after rebooting (shows as just usb com port) and needs resetting before it works. I read somewhere that newer source code should fix this, so downloaded the latest, recompiled and errors everywhere!

I know my code wasn't perfect, and needed rewriting. Just wondering if there's easier ways of doing it now?

All I needed was pushbutton vol+ vol- and mute (much like on extended keyboards, or laptops). Rotary volume might be better. This is system volume, not line volume.

Searches have failed me. Maybe its too simple!

I can upload my code if it helps (please dont judge me on it though)

I think this was the code i got working originally. probably broke all the coding rules and conventions where it wasnt just copied and pasted.

// Constants
// set pin numbers for the buttons:
const int downVolume = 2;     
const int muteVolume = 3;        
const int upVolume = 4;
const int back = 5;
const int forward = 6;
const int playPause = 7;
const int outPin = 13;       // play/pause LED

// long's because the time in ms will become bigger than an int.
long debounce = 20;      // debounce in ms
long holdTime = 2000;    // ms hold period: how long to wait for press+hold event
long time = 0;

// variables
int backVal = 0;       // value read from button
int backLast = 0;       // buffered value of the button's previous state
int forwardVal = 0;
int forwardLast = 0;
long btnDnTime;           // time the button was pressed down
long btnUpTime;           // time the button was released
boolean ignoreUp = false; // whether to ignore the button release because the click+hold was triggered
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin


// initialize the buttons' inputs:
void setup()
{ 
  pinMode(downVolume, INPUT);       
  pinMode(muteVolume, INPUT);       
  pinMode(upVolume, INPUT);
  pinMode(back, INPUT);
  pinMode(forward, INPUT);
  pinMode(playPause, INPUT);
  pinMode(outPin, OUTPUT);
  digitalWrite(back, LOW);
  digitalWrite(forward, LOW);
}


void loop()
{
//**********************Rewind and skip back************************
  // Read the state of the button
  backVal = digitalRead(back);
  
  // Test for button pressed and store the down time
  if (backVal == HIGH && backLast == LOW && (millis() - btnUpTime) > debounce)
  {
    btnDnTime = millis();
  }
  
  // Test for button release and store the up time
  if (backVal == LOW && backLast == HIGH && (millis() - btnDnTime) > debounce)
  {
    if (ignoreUp == false) event1();
      else ignoreUp = false;
      btnUpTime = millis();
  }
  
  // Test for button held down for longer than the hold time
  if (backVal == HIGH && (millis() - btnDnTime) > holdTime)
  {
    event2();
    ignoreUp = true;
    btnDnTime = millis();
  }

backLast = backVal;
//***********************************************************************


//**********************Fforward and skip forward************************
  // Read the state of the button
  forwardVal = digitalRead(forward);
  
  // Test for button pressed and store the down time
  if (forwardVal == HIGH && forwardLast == LOW && (millis() - btnUpTime) > debounce)
  {
    btnDnTime = millis();
  }
  
  // Test for button release and store the up time
  if (forwardVal == LOW && forwardLast == HIGH && (millis() - btnDnTime) > debounce)
  {
    if (ignoreUp == false) event3();
      else ignoreUp = false;
      btnUpTime = millis();
  }
  
  // Test for button held down for longer than the hold time
  if (forwardVal == HIGH && (millis() - btnDnTime) > holdTime)
  {
    event4();
    ignoreUp = true;
    btnDnTime = millis();
  }

forwardLast = forwardVal;
//***********************************************************************


//*****************************Volume************************************
  
  if (digitalRead(downVolume) == HIGH)
  {
    Remote.decrease();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  
  if (digitalRead(muteVolume) == HIGH)
  {
    Remote.mute();
    delay(500);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  
  if (digitalRead(upVolume) == HIGH)
  {
    Remote.increase();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  
//***********************************************************************


//*****************************Play/Pause********************************

reading = digitalRead(playPause);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
    {
      state = LOW;
      Remote.pause();
     // delay(100);     // simple debounce
      Remote.clear(); // Prevent duplicate activation
    }
    else
    {
      state = HIGH;
      Remote.play();
    //  delay(100);     // simple debounce
      Remote.clear(); // Prevent duplicate activation
    }

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
  
}

//=================================================
// Events to trigger by click and press+hold

void event1()
{
  Remote.previous();
  Remote.clear(); // Prevent duplicate activation
}

void event2()
{
  Remote.rewind();
  Remote.clear(); 
}

void event3()
{
  Remote.next();
  Remote.clear(); // Prevent duplicate activation
}

void event4()
{
  Remote.forward();
  Remote.clear(); 
}
  
  
  
  
  
  
  
  
  
  
  
  /*
  
  if (digitalRead(upVolume) == HIGH) {
    Remote.increase();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  
  if (digitalRead(downVolume) == HIGH) {
    Remote.decrease();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  
  if (digitalRead(muteVolume) == HIGH) {
    Remote.mute();
    delay(500);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  
  if (digitalRead(playMedia) == HIGH) {
    Remote.play();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }

  if (digitalRead(pauseMedia) == HIGH) {
    Remote.pause();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }

  if (digitalRead(rewindMedia) == HIGH) {
    Remote.rewind();
    delay(100);     // simple debounce
    Remote.clear(); // Prevent duplicate activation
  }
  */

I think this was the code i got working originally.

No it wasn't. That code uses some class called Remote but it never loads it in as a library so it could never have worked.

the guide in the link above adds the remote class into USBAPI.h and HID.cpp

could those lines be incorporated into my program so it's not affected by variations in source code builds? Or does it not work like that?

Well that would get rid of the error I am seeing but I don’t know if there are any more when that one is gone.

I am using version 1.6.6

Hi, Your problem is you upgraded to 10.

I leave at least 18 months for an OS to mature before even thinking about it. Did going to 10 improve anything?

Tom.... :)

10 is a big improvement over 8 & 8.1 according to my sources. I'm still happy with Win7 Pro myself.

Hi, Ditto Win7, have not had the urge to go win8, someone shoot me if I do.

Tom.... :)

My understanding is that Windows 10 monitors everything you do on any app and reports that back to Microsoft. My guess is that they will use that information to make you pay more for stuff in the future. Do you want that?

yea. still wondering why I did it. There's no benefit, just more mouse clicks for everything and I don't use any 'apps'. Just another layer of GUI to hide everything behind.

As for the code... am I missing a trick? advanced keyboards can manage the system volume, Leonardo can simulate a keyboard... I'd rather not have a program running in the background on the PC, just plug and play. And as I guess 'Remote' is not a core class, its likely to be overwritten in every update