Advertisement
kubbur

Untitled

Aug 13th, 2018
798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  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 button0 = Bounce(0, 10);
  14. Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
  15. Bounce button2 = Bounce(2, 10); // which is appropriate for
  16. Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
  17. Bounce button4 = Bounce(4, 10);
  18. Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
  19. Bounce button6 = Bounce(6, 10); // to rapid touch, you can
  20. Bounce button7 = Bounce(7, 10); // increase this time.
  21. Bounce button8 = Bounce(8, 10);
  22. Bounce button9 = Bounce(9, 10);
  23.  
  24. void setup() {
  25. // Configure the pins for input mode with pullup resistors.
  26. // The pushbuttons connect from each pin to ground. When
  27. // the button is pressed, the pin reads LOW because the button
  28. // shorts it to ground. When released, the pin reads HIGH
  29. // because the pullup resistor connects to +5 volts inside
  30. // the chip. LOW for "on", and HIGH for "off" may seem
  31. // backwards, but using the on-chip pullup resistors is very
  32. // convenient. The scheme is called "active low", and it's
  33. // very commonly used in electronics... so much that the chip
  34. // has built-in pullup resistors!
  35. pinMode(A7, INPUT_PULLDOWN);
  36. pinMode(A6, INPUT_PULLDOWN);
  37. pinMode(A5, INPUT_PULLDOWN);
  38. pinMode(A4, INPUT_PULLDOWN);
  39. pinMode(A3, INPUT_PULLDOWN);
  40.  
  41. }
  42.  
  43. void loop() {
  44. // Update all the buttons. There should not be any long
  45. // delays in loop(), so this runs repetitively at a rate
  46. // faster than the buttons could be pressed and released.
  47. button0.update();
  48. button1.update();
  49. button2.update();
  50. button3.update();
  51. button4.update();
  52. button5.update();
  53. button6.update();
  54. button7.update();
  55. button8.update();
  56. button9.update();
  57.  
  58. // Check each button for "falling" edge.
  59. // Type a message on the Keyboard when each button presses
  60. // Update the Joystick buttons only upon changes.
  61. // falling = high (not pressed - voltage from pullup resistor)
  62. // to low (pressed - button connects pin to ground)
  63. if (button0.fallingEdge()) {
  64. Keyboard.println("B0 press");
  65. }
  66. if (button1.fallingEdge()) {
  67. Keyboard.println("B1 press");
  68. }
  69. if (button2.fallingEdge()) {
  70. Keyboard.println("B2 press");
  71. }
  72. if (button3.fallingEdge()) {
  73. Keyboard.println("B3 press");
  74. }
  75. if (button4.fallingEdge()) {
  76. Keyboard.println("B4 press");
  77. }
  78. if (button5.fallingEdge()) {
  79. Keyboard.println("B5 press");
  80. }
  81. if (button6.fallingEdge()) {
  82. Keyboard.println("B6 press");
  83. }
  84. if (button7.fallingEdge()) {
  85. Keyboard.println("B7 press");
  86. }
  87. if (button8.fallingEdge()) {
  88. Keyboard.println("B8 press");
  89. }
  90. if (button9.fallingEdge()) {
  91. Keyboard.println("B9 press");
  92. }
  93.  
  94. // Check each button for "rising" edge
  95. // Type a message on the Keyboard when each button releases.
  96. // For many types of projects, you only care when the button
  97. // is pressed and the release isn't needed.
  98. // rising = low (pressed - button connects pin to ground)
  99. // to high (not pressed - voltage from pullup resistor)
  100. if (button0.risingEdge()) {
  101. Keyboard.println("B0 release");
  102. }
  103. if (button1.risingEdge()) {
  104. Keyboard.println("B1 release");
  105. }
  106. if (button2.risingEdge()) {
  107. Keyboard.println("B2 release");
  108. }
  109. if (button3.risingEdge()) {
  110. Keyboard.println("B3 release");
  111. }
  112. if (button4.risingEdge()) {
  113. Keyboard.println("B4 release");
  114. }
  115. if (button5.risingEdge()) {
  116. Keyboard.println("B5 release");
  117. }
  118. if (button6.risingEdge()) {
  119. Keyboard.println("B6 release");
  120. }
  121. if (button7.risingEdge()) {
  122. Keyboard.println("B7 release");
  123. }
  124. if (button8.risingEdge()) {
  125. Keyboard.println("B8 release");
  126. }
  127. if (button9.risingEdge()) {
  128. Keyboard.println("B9 release");
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement