Teensy keyboard program issues

Status
Not open for further replies.

dbxme2

New member
Hi Everyone,

This is my first Teensy project. I'm basing myself from the example code that is provided to turn a teensy into a 4 key keyboard.

The problem I'm coming into is when I hit a key in my project, it will return the proper key assigned to that button, but it will also return the "enter" key on release.

The wiring configuration is simple, I'll use B0 as an example (B0 -> mx switch -> Diode -> GND)

any insight would be much appreciated!!!!!!! I'm desperate and looked everywhere for answers lol.

Thanks,
DBX

Here's my code:
/* Buttons to USB Keyboard Example

You must select Keyboard 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(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);
Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 10); // to rapid touch, you can
Bounce button7 = Bounce(7, 10); // increase this time.
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);

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
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_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();
button6.update();
button7.update();
button8.update();
button9.update();

// Check each button for "falling" edge.
// Type a message on the Keyboard 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 (button1.fallingEdge()) {
Keyboard.press(KEY_F11);
}
if (button3.fallingEdge()) {
Keyboard.press(KEY_UP);
}
if (button5.fallingEdge()) {
Keyboard.press(KEY_DOWN);
}
if (button7.fallingEdge()) {
Keyboard.press(KEY_ENTER);
}

// Check each button for "rising" edge
// Type a message on the Keyboard when each button releases.
// For many types of projects, you only care when the button
// is pressed and the release isn't needed.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button1.risingEdge()) {
Keyboard.println("");
}
if (button3.risingEdge()) {
Keyboard.println("");
}
if (button5.risingEdge()) {
Keyboard.println("");
}
if (button7.risingEdge()) {
Keyboard.println("");
}
}
 
Code:
if (button1.risingEdge()) {
Keyboard.println("");
}
if (button3.risingEdge()) {
Keyboard.println("");
}
if (button5.risingEdge()) {
Keyboard.println("");
}
if (button7.risingEdge()) {
Keyboard.println("");
}
This code sends a linefeed (enter) whenever a rising edge is seen on button 1, 3, 5 or 7.
You can just comment it all out if you don't want anything done when a button is released.

Pete
 
Thanks el_supremo!

That helped to remove the enter being returned after a key stroke.

I'm guessing I have to enter something in the void loop to avoid the key to continue being engaged after its been released.

Code:
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();
  button6.update();
  button7.update();
  button8.update();
  button9.update();
 
Sorry, if there are examples you guys can point me too of completed projects I would be happy to review their code :S
 
I figured it out!

Its good now, had to pair the release with the press

Code:
  if (button1.fallingEdge()) {
    Keyboard.press(KEY_F11);
    Keyboard.release(KEY_F11);
  }
  if (button3.fallingEdge()) {
    Keyboard.press(KEY_UP);
    Keyboard.release(KEY_UP);
  }
  if (button5.fallingEdge()) {
    Keyboard.press(KEY_DOWN);
    Keyboard.release(KEY_DOWN);
  }
  if (button7.fallingEdge()) {
    Keyboard.press(KEY_ENTER);
    Keyboard.release(KEY_ENTER);
  }

Thanks again!
DBX
 
I think what you do is add a Keyboard.release to match each Keyboard.press.
Like this:
Code:
if (button1.risingEdge()) {
Keyboard.release(KEY_F11);
}
and do a similar thing for the other buttons. I got this from the example in Examples|Teensy|USB_Keyboard|MediaButtons

Ah, I see you've figured it out. Except that you have already sent .press when the falling edge is detected so you only need to send release when rising edge is detected.

Pete
 
Last edited:
Status
Not open for further replies.
Back
Top