SHARE
TWEET

Untitled

kubbur Aug 16th, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Buttons to USB Serial Example
  2.  
  3.    You must select Serial 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(A3, 14);
  14. Bounce button1 = Bounce(A4, 14);  // 10 = 10 ms debounce time
  15. Bounce button2 = Bounce(A5, 14);  // which is appropriate for
  16. Bounce button3 = Bounce(A6, 14);  // most mechanical pushbuttons
  17. Bounce button4 = Bounce(A7, 14);
  18. Bounce button5 = Bounce(5, 10);  // if a button is too "sensitive"
  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(0, INPUT_PULLUP);
  33.   pinMode(1, INPUT_PULLUP);
  34.   pinMode(2, INPUT_PULLUP);
  35.   pinMode(3, INPUT_PULLUP);
  36.   pinMode(4, INPUT_PULLUP);
  37.   pinMode(5, INPUT_PULLUP);
  38.   pinMode(6, INPUT_PULLUP);  // Teensy++ LED, may need 1k resistor pullup
  39. }
  40.  
  41. void loop() {
  42.   // Update all the buttons.  There should not be any long
  43.   // delays in loop(), so this runs repetitively at a rate
  44.   // faster than the buttons could be pressed and released.
  45.   button0.update();
  46.   button1.update();
  47.   button2.update();
  48.   button3.update();
  49.   button4.update();
  50.   button5.update();
  51.  
  52.  
  53.   // Check each button for "falling" edge.
  54.   // Type a message on the Serial when each button presses
  55.   // Update the Joystick buttons only upon changes.
  56.   // falling = high (not pressed - voltage from pullup resistor)
  57.   //           to low (pressed - button connects pin to ground)
  58.   if (button0.fallingEdge()) {
  59.     Serial.println("q");
  60.     Keyboard.print("q");
  61.   }
  62.   if (button1.fallingEdge()) {
  63.     Serial.println("3");
  64.     Keyboard.print("3");
  65.   }
  66.   if (button2.fallingEdge()) {
  67.     Serial.println("2");
  68.     Keyboard.print("2");
  69.   }
  70.   if (button3.fallingEdge()) {
  71.     Serial.println("1");
  72.     Keyboard.print("1");
  73.   }
  74.   if (button4.fallingEdge()) {
  75.     Serial.println("q");
  76.     Keyboard.print("q");
  77.   }
  78.   if (button5.fallingEdge()) {
  79.     Serial.println("r");
  80.     Keyboard.print("r");
  81.   }
  82. }
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