Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Teensy push button

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    1

    Teensy push button

    Hello I would like some help with some code
    I have teensy 2.0 and I want to push a button and then it print a letter on the screen so I would short pin 0 with gnd and the print a letter (a) for example
    But I want to print it once and have no mechanical chatter ?

    So far I have this but it just when pressed just prints a string of numbers

    /* Buttons to USB Keyboard

    You must select Keyboard from the "Tools > USB Type" menu
    */

    #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(0, 10);
    Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
    Bounce button2 = Bounce(2, 10); // which is appropriate for
    Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
    Bounce button4 = Bounce(4, 10);

    void setup() {
    pinMode(0, INPUT_PULLUP);
    pinMode(1, INPUT_PULLUP);
    pinMode(2, INPUT_PULLUP);
    pinMode(3, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
    }
    void SendKey(int key) {

    Keyboard.set_modifier(MODIFIERKEY_CTRL);
    Keyboard.send_now();

    Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT);
    Keyboard.send_now();

    Keyboard.set_key1(key);
    Keyboard.send_now();

    Keyboard.set_modifier(0);
    Keyboard.set_key1(0);
    Keyboard.send_now();
    }

    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();

    // Check each button for "falling" edge.
    // Type a message on the Keyboard when each button presses
    // falling = high (not pressed - voltage from pullup resistor)
    // to low (pressed - button connects pin to ground)
    if (button0.fallingEdge()) {
    SendKey(KEY_F1);
    }
    if (button1.fallingEdge()) {
    SendKey(KEY_F2);
    }
    if (button2.fallingEdge()) {
    SendKey(KEY_F3);
    }
    if (button3.fallingEdge()) {
    SendKey(KEY_F4);
    }
    if (button4.fallingEdge()) {
    SendKey(KEY_F5);
    }
    }

  2. #2
    Junior Member
    Join Date
    Jan 2017
    Posts
    11
    When you say that it just "prints a string of numbers"... Do you mean it prints several characters in a row due to button chatter or does it continue sending characters constantly?
    If it is chatter, take a look at the Bounce2 library and you may want to change library to LOCKOUT method using #define BOUNCE_LOCK_OUT. Check out comments in library. or google that to see the difference in dampening methods that library uses to prevent chatter.
    If it is constantly sending, it may be due to you not having a "Repeat delay timer" programmed in as a way to prevent it from continually sending characters. I guess you were relying on the button.fallingEdge() function to do this. I haven't looked at the Bounce library but that may or may not repeat for some reason.. It shouldn't repeat but just take a look at code in that library to check.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •