Teensy 2.0 and button lag

Hello, I'm trying to make a USB HID with Teensy 2.0. It's a "simple" 6 button controller. 5 of the buttons activate a function which send keyboard presses. The main ones that are sent are the up arrow, the down arrow, Page Down, ctrl+b and ctrl+c. The buttons are wired from 5v to digital 1-5, a red LED then a 1k resistor. The buttons work, as the LEDs indicate. When I plug in my device, the functions are called sporadically. (e.g., Sometimes the cursor moves up and down when the button is pressed, other times, nothing happens and for some odd reason, ctrl+b is randomly, albeit constantly, being sent whether or not the button is being pressed.)

I've tried using the buttons without a form of debouncing and I am currently using the Bounce library with no difference. I've tried using the onboard resistors to no avail. I've done button1.fallingEdge and button1.risingEdge. Either way, it's still sporadic.

Any ideas?

If the button is between +5 and the Arduino pin you need a pull-down resistor between the Arduino pin and Ground.

Yeah. Sorry, I meant to mention that the 1k resistor is from button to ground. Sorry

You forgot to post your code. (please use
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags)

Dang. I’ll eventually get the hang of this… :slight_smile: I’ll just post the pertinent code, I have some fancy RGB LED code, but I don’t want to overload your screen with like 200 lines of code.

#include <Bounce.h>

// setup the button pins
Bounce button1 = Bounce(1,5);
Bounce button2 = Bounce(2,5);
Bounce button3 = Bounce(3,5);
Bounce button4 = Bounce(4,5);
Bounce button5 = Bounce(5,5);

void setup(){
  // make the buttons inputs and use the onboard pullup resistors
  pinMode(1, INPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
}


void loop(){
  // Update the buttons
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  
  /* In Easy Worship Mode:
      Light Color = Blue
      Button1 = up          (aka Up Arrow)
      Button2 = down        (aka Down Arrow)
      Button3 = PageDown    (aka Go Live)
      Button4 = ctrlB       (aka Black)
      Button5 = ctrlC      (aka Clear) */
  if (selectedMode == 1) {
      if (button1.risingEdge()) up();
      if (button2.risingEdge()) down();
      if (button3.risingEdge()) pageDown();
      if (button4.risingEdge()) ctrlB();
      if (button5.risingEdge()) ctrlC();
}

My end goal is to have the five buttons work in different modes. (e.g., one mode for one program, another for another program.) Thus the selectedMode statement above.

Bounce button1 = Bounce(1,5);

5 milliseconds is too short. You should be using values between 20 and 200.

urnumdei:
I don't want to overload your screen with like 200 lines of code.

I have a sneaky suspicion my screen is not going to care about the size. It gets paid the same amount if it's displaying a one line program or a 200 line program.

[quote author=Coding Badly link=topic=103655.msg777643#msg777643 date=1335733756]
I have a sneaky suspicion my screen is not going to care about the size. It gets paid the same amount if it's displaying a one line program or a 200 line program.[/quote]

lol. I figured, but why read about an LED when the buttons are being weird?

I originally did 10, which was in the example sketch. It didn't dawn on me to try to lengthen the time.

I set it to 200 which made it much better. About 90% better, but that's an arbitrary number. :slight_smile:

I'm still having issues with the ctrl+b function. It seems like there's like, too many requests for the keypresses, so the controller is overloaded. Does that make sense?

Does that make sense?

No.