Advertisement
emkay443

Teensy Multimedia Keyboard

Mar 4th, 2014
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. /* Buttons to USB Keyboard Example - Special Media Player Keys
  2.  
  3.    You must select Keyboard from the "Tools > USB Type" menu
  4.  
  5.    This example code is in the public domain.
  6. */
  7.  
  8. #include <Bounce.h>
  9.  
  10. // Create Bounce objects for each button.  The Bounce object
  11. // automatically deals with contact chatter or "bounce", and
  12. // it makes detecting changes very simple.
  13. Bounce button0 = Bounce(0, 10);
  14. Bounce button1 = Bounce(1, 10);  // 10 ms debounce time is appropriate
  15. Bounce button2 = Bounce(2, 10);  // for most mechanical pushbuttons
  16. Bounce button3 = Bounce(3, 10);
  17. Bounce button4 = Bounce(4, 10);  // if a button is too "sensitive"
  18. Bounce button5 = Bounce(5, 10);  // you can increase this time.
  19. Bounce button6 = Bounce(6, 10);
  20.  
  21. void setup() {
  22.   // Configure the pins for input mode with pullup resistors.
  23.   // The pushbuttons connect from each pin to ground.  When
  24.   // the button is pressed, the pin reads LOW because the button
  25.   // shorts it to ground.  When released, the pin reads HIGH
  26.   // because the pullup resistor connects to +5 volts inside
  27.   // the chip.Hello World 2
  28.  
  29.   pinMode(0, INPUT_PULLUP);
  30.   pinMode(1, INPUT_PULLUP);
  31.   pinMode(2, INPUT_PULLUP);
  32.   pinMode(3, INPUT_PULLUP);
  33.   pinMode(4, INPUT_PULLUP);
  34.   pinMode(5, INPUT_PULLUP);
  35.   pinMode(6, INPUT_PULLUP);
  36. }
  37.  
  38. void loop() {
  39.   // Update all the buttons.  There should not be any long
  40.   // delays in loop(), so this runs repetitively at a rate
  41.   // faster than the buttons could be pressed and released.
  42.   button0.update();
  43.   button1.update();
  44.   button2.update();
  45.   button3.update();
  46.   button4.update();
  47.   button5.update();
  48.   button6.update();
  49.  
  50.   // Check each button for "falling" edge.
  51.   // falling = high (not pressed - voltage from pullup resistor)
  52.   //           to low (pressed - button connects pin to ground)
  53.   if (button0.fallingEdge()) {
  54.     Keyboard.set_media(KEY_MEDIA_PLAY_PAUSE);
  55.     Keyboard.send_now();    // send the button press
  56.   }
  57.   if (button0.risingEdge()){
  58.     Keyboard.set_media(0);
  59.     Keyboard.send_now();
  60.   }
  61.   if (button1.fallingEdge()) {
  62.     Keyboard.set_media(KEY_MEDIA_STOP);
  63.     Keyboard.send_now();
  64.   }
  65.   if (button1.risingEdge()){
  66.     Keyboard.set_media(0);
  67.     Keyboard.send_now();
  68.   }
  69.   if (button2.fallingEdge()) {
  70.     Keyboard.set_media(KEY_MEDIA_PREV_TRACK);
  71.     Keyboard.send_now();
  72.   }
  73.   if (button2.risingEdge()){
  74.     Keyboard.set_media(0);
  75.     Keyboard.send_now();
  76.   }
  77.   if (button3.fallingEdge()) {
  78.     Keyboard.set_media(KEY_MEDIA_NEXT_TRACK);
  79.     Keyboard.send_now();
  80.   }
  81.   if (button3.risingEdge()){
  82.     Keyboard.set_media(0);
  83.     Keyboard.send_now();
  84.   }
  85.   if (button4.fallingEdge()) {
  86.     Keyboard.set_media(KEY_MEDIA_VOLUME_DEC);
  87.     Keyboard.send_now();
  88.   }
  89.   if (button4.risingEdge()){
  90.     Keyboard.set_media(0);
  91.     Keyboard.send_now();
  92.   }
  93.   if (button5.fallingEdge()) {
  94.     Keyboard.set_media(KEY_MEDIA_VOLUME_INC);
  95.     Keyboard.send_now();
  96.   }
  97.   if (button5.risingEdge()){
  98.     Keyboard.set_media(0);
  99.     Keyboard.send_now();
  100.   }
  101.   if (button6.fallingEdge()) {
  102.     Keyboard.set_media(KEY_MEDIA_MUTE);
  103.     Keyboard.send_now();
  104.   }
  105.   if (button6.risingEdge()){
  106.     Keyboard.set_media(0);
  107.     Keyboard.send_now();
  108.   }
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement