SHARE
TWEET

Teensy 2.0 23 buttons 1 joystick

a guest May 24th, 2015 459 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Basic USB Joystick Example
  2.    Teensy becomes a USB joystick
  3.  
  4.    You must select Joystick from the "Tools > USB Type" menu
  5.  
  6.    Pushbuttons should be connected to digital pins 0 and 1.
  7.    Wire each button between the digital pin and ground.
  8.    Potentiometers should be connected to analog inputs 0 to 1.
  9.  
  10.    This example code is in the public domain.
  11. */
  12. #include <Bounce.h>
  13.  
  14. // Create Bounce objects for each button.  The Bounce object
  15. // automatically deals with contact chatter or "bounce", and
  16. // it makes detecting changes very simple.
  17. Bounce button0 = Bounce(0, 10);
  18. Bounce button1 = Bounce(1, 10);  // 10 = 10 ms debounce time
  19. Bounce button2 = Bounce(2, 10);  // which is appropriate for
  20. Bounce button3 = Bounce(3, 10);  // most mechanical pushbuttons
  21. Bounce button4 = Bounce(4, 10);
  22. Bounce button5 = Bounce(5, 10);
  23. Bounce button6 = Bounce(6, 10);
  24. Bounce button7 = Bounce(7, 10);
  25. Bounce button8 = Bounce(8, 10);
  26. Bounce button9 = Bounce(9, 10);
  27. Bounce button10 = Bounce(10, 10);
  28. Bounce button11 = Bounce(11, 10);  // 10 = 10 ms debounce time
  29. Bounce button12 = Bounce(12, 10);  // which is appropriate for
  30. Bounce button13 = Bounce(13, 10);  // most mechanical pushbuttons
  31. Bounce button14 = Bounce(14, 10);
  32. Bounce button15 = Bounce(15, 10);
  33. Bounce button16 = Bounce(16, 10);
  34. Bounce button17 = Bounce(17, 10);
  35. Bounce button18 = Bounce(18, 10);
  36. Bounce button19 = Bounce(19, 10);
  37. Bounce button22 = Bounce(22, 10);
  38. Bounce button23 = Bounce(23, 10);
  39. Bounce button24 = Bounce(24, 10);
  40.  
  41. void setup() {
  42.   pinMode(0, INPUT_PULLUP);
  43.   pinMode(1, INPUT_PULLUP);
  44.   pinMode(2, INPUT_PULLUP);
  45.   pinMode(3, INPUT_PULLUP);
  46.   pinMode(4, INPUT_PULLUP);
  47.   pinMode(5, INPUT_PULLUP);
  48.   pinMode(6, INPUT_PULLUP);
  49.   pinMode(7, INPUT_PULLUP);
  50.   pinMode(8, INPUT_PULLUP);
  51.   pinMode(9, INPUT_PULLUP);
  52.   pinMode(10, INPUT_PULLUP);
  53.   pinMode(11, INPUT_PULLUP);
  54.   pinMode(12, INPUT_PULLUP);
  55.   pinMode(13, INPUT_PULLUP);
  56.   pinMode(14, INPUT_PULLUP);
  57.   pinMode(15, INPUT_PULLUP);
  58.   pinMode(16, INPUT_PULLUP);
  59.   pinMode(17, INPUT_PULLUP);
  60.   pinMode(18, INPUT_PULLUP);
  61.   pinMode(19, INPUT_PULLUP);
  62.   pinMode(22, INPUT_PULLUP);
  63.   pinMode(23, INPUT_PULLUP);  
  64.   pinMode(24, INPUT_PULLUP);
  65. }
  66.  
  67. void loop() {
  68.   // read analog inputs and set X-Y position
  69.   Joystick.X(analogRead(0));
  70.   Joystick.Y(analogRead(1));
  71.  
  72.   // Update all the buttons.  There should not be any long
  73.   // delays in loop(), so this runs repetitively at a rate
  74.   // faster than the buttons could be pressed and released.
  75.   button0.update();
  76.   button1.update();
  77.   button2.update();
  78.   button3.update();
  79.   button4.update();
  80.   button5.update();
  81.   button6.update();
  82.   button7.update();
  83.   button8.update();
  84.   button9.update();
  85.   button10.update();
  86.   button11.update();
  87.   button12.update();
  88.   button13.update();
  89.   button14.update();
  90.   button15.update();
  91.   button16.update();
  92.   button17.update();
  93.   button18.update();
  94.   button19.update();
  95.   button22.update();
  96.   button23.update();
  97.   button24.update();
  98.  
  99.   // Check each button for "falling" edge.
  100.   // Update the Joystick buttons only upon changes.
  101.   // falling = high (not pressed - voltage from pullup resistor)
  102.   //           to low (pressed - button connects pin to ground)
  103.   if (button0.fallingEdge()) {
  104.     Joystick.button(1, 1);
  105.   }
  106.   if (button1.fallingEdge()) {
  107.     Joystick.button(2, 1);
  108.   }
  109.   if (button2.fallingEdge()) {
  110.     Joystick.button(3, 1);
  111.   }
  112.   if (button3.fallingEdge()) {
  113.     Joystick.button(4, 1);
  114.   }
  115.   if (button4.fallingEdge()) {
  116.     Joystick.button(5, 1);
  117.   }
  118.   if (button5.fallingEdge()) {
  119.     Joystick.button(6, 1);
  120.   }
  121.   if (button6.fallingEdge()) {
  122.     Joystick.button(7, 1);
  123.   }
  124.   if (button7.fallingEdge()) {
  125.     Joystick.button(8, 1);
  126.   }
  127.   if (button8.fallingEdge()) {
  128.     Joystick.button(9, 1);
  129.   }
  130.   if (button9.fallingEdge()) {
  131.     Joystick.button(10, 1);
  132.   }
  133.   if (button10.fallingEdge()) {
  134.     Joystick.button(11, 1);
  135.   }
  136.   if (button11.fallingEdge()) {
  137.     Joystick.button(12, 1);
  138.   }
  139.   if (button12.fallingEdge()) {
  140.     Joystick.button(13, 1);
  141.   }
  142.   if (button13.fallingEdge()) {
  143.     Joystick.button(14, 1);
  144.   }
  145.   if (button14.fallingEdge()) {
  146.     Joystick.button(15, 1);
  147.   }
  148.   if (button15.fallingEdge()) {
  149.     Joystick.button(16, 1);
  150.   }
  151.   if (button16.fallingEdge()) {
  152.     Joystick.button(17, 1);
  153.   }
  154.   if (button17.fallingEdge()) {
  155.     Joystick.button(18, 1);
  156.   }
  157.   if (button18.fallingEdge()) {
  158.     Joystick.button(19, 1);
  159.   }
  160.   if (button19.fallingEdge()) {
  161.     Joystick.button(20, 1);
  162.   }
  163.   if (button22.fallingEdge()) {
  164.     Joystick.button(21, 1);
  165.   }
  166.   if (button23.fallingEdge()) {
  167.     Joystick.button(22, 1);
  168.   }
  169.   if (button24.fallingEdge()) {
  170.     Joystick.button(23, 1);
  171.   }
  172.   // Check each button for "rising" edge
  173.   // Update the Joystick buttons only upon changes.
  174.   // rising = low (pressed - button connects pin to ground)
  175.   //          to high (not pressed - voltage from pullup resistor)
  176.   if (button0.risingEdge()) {
  177.     Joystick.button(1, 0);
  178.   }
  179.   if (button1.risingEdge()) {
  180.     Joystick.button(2, 0);
  181.   }
  182.   if (button2.risingEdge()) {
  183.     Joystick.button(3, 0);
  184.   }
  185.   if (button3.risingEdge()) {
  186.     Joystick.button(4, 0);
  187.   }
  188.   if (button4.risingEdge()) {
  189.     Joystick.button(5, 0);
  190.   }
  191.   if (button5.risingEdge()) {
  192.     Joystick.button(6, 0);
  193.   }
  194.   if (button6.risingEdge()) {
  195.     Joystick.button(7, 0);
  196.   }
  197.   if (button7.risingEdge()) {
  198.     Joystick.button(8, 0);
  199.   }
  200.   if (button8.risingEdge()) {
  201.     Joystick.button(9, 0);
  202.   }
  203.   if (button9.risingEdge()) {
  204.     Joystick.button(10, 0);
  205.   }
  206.   if (button10.risingEdge()) {
  207.     Joystick.button(11, 0);
  208.   }
  209.   if (button11.risingEdge()) {
  210.     Joystick.button(12, 0);
  211.   }
  212.   if (button12.risingEdge()) {
  213.     Joystick.button(13, 0);
  214.   }
  215.   if (button13.risingEdge()) {
  216.     Joystick.button(14, 0);
  217.   }
  218.   if (button14.risingEdge()) {
  219.     Joystick.button(15, 0);
  220.   }
  221.   if (button15.risingEdge()) {
  222.     Joystick.button(16, 0);
  223.   }
  224.   if (button16.risingEdge()) {
  225.     Joystick.button(17, 0);
  226.   }
  227.   if (button17.risingEdge()) {
  228.     Joystick.button(18, 0);
  229.   }
  230.   if (button18.risingEdge()) {
  231.     Joystick.button(19, 0);
  232.   }
  233.   if (button19.risingEdge()) {
  234.     Joystick.button(20, 0);
  235.   }  
  236.   if (button22.risingEdge()) {
  237.     Joystick.button(21, 0);
  238.   }  
  239.   if (button23.risingEdge()) {
  240.     Joystick.button(22, 0);
  241.   }
  242.   if (button24.risingEdge()) {
  243.     Joystick.button(23, 0);
  244.   }
  245. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top