Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Buttons to USB Serial Example
- You must select Serial from the "Tools > USB Type" menu
- This example code is in the public domain.
- */
- #include <Bounce.h>
- // Create Bounce objects for each button. The Bounce object
- // automatically deals with contact chatter or "bounce", and
- // it makes detecting changes very simple.
- Bounce button0 = Bounce(A3, 14);
- Bounce button1 = Bounce(A4, 14); // 10 = 10 ms debounce time
- Bounce button2 = Bounce(A5, 14); // which is appropriate for
- Bounce button3 = Bounce(A6, 14); // most mechanical pushbuttons
- Bounce button4 = Bounce(A7, 14);
- Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
- void setup() {
- // Configure the pins for input mode with pullup resistors.
- // The pushbuttons connect from each pin to ground. When
- // the button is pressed, the pin reads LOW because the button
- // shorts it to ground. When released, the pin reads HIGH
- // because the pullup resistor connects to +5 volts inside
- // the chip. LOW for "on", and HIGH for "off" may seem
- // backwards, but using the on-chip pullup resistors is very
- // convenient. The scheme is called "active low", and it's
- // very commonly used in electronics... so much that the chip
- // has built-in pullup resistors!
- pinMode(0, INPUT_PULLUP);
- pinMode(1, INPUT_PULLUP);
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(4, INPUT_PULLUP);
- pinMode(5, INPUT_PULLUP);
- pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
- }
- void loop() {
- // Update all the buttons. There should not be any long
- // delays in loop(), so this runs repetitively at a rate
- // faster than the buttons could be pressed and released.
- button0.update();
- button1.update();
- button2.update();
- button3.update();
- button4.update();
- button5.update();
- // Check each button for "falling" edge.
- // Type a message on the Serial when each button presses
- // Update the Joystick buttons only upon changes.
- // falling = high (not pressed - voltage from pullup resistor)
- // to low (pressed - button connects pin to ground)
- if (button0.fallingEdge()) {
- Serial.println("q");
- Keyboard.print("q");
- }
- if (button1.fallingEdge()) {
- Serial.println("3");
- Keyboard.print("3");
- }
- if (button2.fallingEdge()) {
- Serial.println("2");
- Keyboard.print("2");
- }
- if (button3.fallingEdge()) {
- Serial.println("1");
- Keyboard.print("1");
- }
- if (button4.fallingEdge()) {
- Serial.println("q");
- Keyboard.print("q");
- }
- if (button5.fallingEdge()) {
- Serial.println("r");
- Keyboard.print("r");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement