daily pastebin goal
50%
SHARE
TWEET

Volume controls that work for the Arduino Leonardo

moggiex Feb 11th, 2016 222 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Arduino USB Keyboard HID - Media Controls that work, Volume up, volume down and .... mute!
  2.  * Volume+/Volume-/Mute keys
  3.  */
  4.  
  5. #include "HID-Project.h"
  6.  
  7. #define PIN_VOLUME_UP 5
  8. #define PIN_VOLUME_DOWN 6
  9. #define PIN_MUTE 7
  10.  
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(9600);
  15.   pinMode(PIN_VOLUME_UP, INPUT);
  16.   pinMode(PIN_VOLUME_DOWN, INPUT);
  17.   pinMode(PIN_MUTE, INPUT);
  18.  
  19.   // enable internal pull-ups
  20.   digitalWrite(PIN_VOLUME_UP, INPUT_PULLUP);
  21.   digitalWrite(PIN_VOLUME_DOWN, INPUT_PULLUP);
  22.   digitalWrite(PIN_MUTE, INPUT_PULLUP);
  23.  
  24.   // Sends a clean report to the host. This is important on any Arduino type.
  25.   //Keyboard.begin();
  26.   Consumer.begin();
  27. }
  28.  
  29. void loop()
  30. {
  31.   if (digitalRead(PIN_VOLUME_UP) != 1) {
  32.     //Keyboard.write(KEY_VOLUME_UP);
  33.     Consumer.write(MEDIA_VOLUME_UP);
  34.     //Keyboard.write(HID_KEYBOARD_VOLUME_UP);
  35.     //Keyboard.write(KEY_MEDIA_VOLUME_INC);
  36.     //Keyboard.println("Hello World!");
  37.     Serial.println("Volume Up");
  38.     delay(300);
  39.     //digitalWrite(PIN_VOLUME_UP, LOW);    
  40.   }
  41.  
  42.   if (state = digitalRead(PIN_VOLUME_DOWN) != 1) {
  43.     //Keyboard.write(KEY_VOLUME_DOWN);
  44.     Consumer.write(MEDIA_VOLUME_DOWN);
  45.     //Keyboard.write(HID_KEYBOARD_VOLUME_UP);
  46.     //Keyboard.write(KEY_MEDIA_VOLUME_DEC);
  47.    
  48.     Serial.println("Volume Down");
  49.     delay(300);
  50.     //digitalWrite(PIN_VOLUME_DOWN, LOW);
  51.   }
  52.   if (digitalRead(PIN_MUTE) != 1) {
  53.     //Keyboard.write(KEY_VOLUME_MUTE);
  54.     //Keyboard.write(KEY_MUTE);
  55.    
  56.     Consumer.write(MEDIA_VOLUME_MUTE);
  57.     //Keyboard.write(HID_KEYBOARD_MUTE);
  58.     Serial.println("Mute");
  59.     delay(300);
  60.     //digitalWrite(HID_KEYBOARD_MUTE, LOW);
  61.   }
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top