SHARE
TWEET

Switch Tester Teensy2.0 Code

a guest Mar 7th, 2015 435 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Buttons to USB Keyboard Example
  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 button13 = Bounce(13, 10);
  14. Bounce button14 = Bounce(14, 10);  // 10 = 10 ms debounce time
  15. Bounce button15 = Bounce(15, 10);  // which is appropriate for
  16. Bounce button16 = Bounce(16, 10);  // most mechanical pushbuttons
  17. Bounce button17 = Bounce(17, 10);
  18. Bounce button18 = Bounce(18, 10);  //change 13-18 to your pins #s
  19.  
  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.  LOW for "on", and HIGH for "off" may seem
  28.   // backwards, but using the on-chip pullup resistors is very
  29.   // convenient.  The scheme is called "active low", and it's
  30.   // very commonly used in electronics... so much that the chip
  31.   // has built-in pullup resistors!
  32.   pinMode(13, INPUT_PULLUP);
  33.   pinMode(14, INPUT_PULLUP);
  34.   pinMode(15, INPUT_PULLUP);
  35.   pinMode(16, INPUT_PULLUP);
  36.   pinMode(17, INPUT_PULLUP);
  37.   pinMode(18, INPUT_PULLUP);
  38. }
  39.  
  40. void loop() {
  41.   // Update all the buttons.  There should not be any long
  42.   // delays in loop(), so this runs repetitively at a rate
  43.   // faster than the buttons could be pressed and released.
  44.   button13.update();
  45.   button14.update();
  46.   button15.update();
  47.   button16.update();
  48.   button17.update();
  49.   button18.update();
  50.  
  51.   // Check each button for "falling" edge.
  52.   // Type a message on the Keyboard when each button presses
  53.   // Update the Joystick buttons only upon changes.
  54.   // falling = high (not pressed - voltage from pullup resistor)
  55.   //           to low (pressed - button connects pin to ground)
  56.   //tldr=what action do you want to execute on a key press
  57.  
  58.   if (button13.fallingEdge()) {      //green switch
  59.     Keyboard.set_modifier(0);
  60.     Keyboard.set_key1(0);
  61.     for(int n=1; n<5; n++){
  62.       Keyboard.print("Reason ");
  63.       Keyboard.println(n);
  64.       Keyboard.send_now();
  65.       Keyboard.set_key1(KEY_TAB);
  66.       Keyboard.send_now();
  67.       Keyboard.set_key1(0);
  68.       Keyboard.send_now();
  69.       delay(100);
  70.       }
  71.   }    
  72.   if (button14.fallingEdge()) {        //blue switch
  73.     Keyboard.set_modifier(MODIFIERKEY_CTRL);
  74.     Keyboard.set_key2(KEY_A);
  75.     Keyboard.send_now();
  76.     delay(50);
  77.     Keyboard.set_modifier(0);
  78.     Keyboard.set_key2(0);
  79.     Keyboard.send_now();
  80.   }
  81.   if (button15.fallingEdge()) {      //clear switch
  82.     Keyboard.set_modifier(MODIFIERKEY_CTRL);
  83.     Keyboard.set_key3(KEY_C);
  84.     Keyboard.send_now();
  85.     delay(50);
  86.     Keyboard.set_modifier(0);
  87.     Keyboard.set_key3(0);
  88.     Keyboard.send_now();      
  89.   }
  90.   if (button16.fallingEdge()) {      //brown switch
  91.     Keyboard.set_modifier(MODIFIERKEY_CTRL);
  92.     Keyboard.set_key4(KEY_V);
  93.     Keyboard.send_now();
  94.     delay(50);
  95.     Keyboard.set_modifier(0);
  96.     Keyboard.set_key4(0);
  97.     Keyboard.send_now();
  98.   }
  99.   if (button17.fallingEdge()) {      //black switch
  100.     Keyboard.set_modifier(MODIFIERKEY_ALT);
  101.     Keyboard.send_now();
  102.     Keyboard.set_key5(KEY_TAB);
  103.     Keyboard.send_now();
  104.     delay(50);
  105.     Keyboard.set_key5(0);
  106.     Keyboard.send_now();  
  107.   }  
  108.  
  109.  //risingEdge=what action do you want to execute on a key release
  110.  if (button17.risingEdge()){
  111.     Keyboard.set_modifier(0);
  112.     Keyboard.send_now();
  113.  }
  114.    
  115.   if (button18.fallingEdge()) {        //red switch
  116.     Keyboard.set_key6(KEY_TAB);
  117.     Keyboard.send_now();
  118.     delay(50);
  119.     Keyboard.set_key6(0);
  120.     Keyboard.send_now();
  121.     }
  122.   }
RAW Paste Data
Top