button state change issue

I’m running the code found at http://arduino.cc/en/Tutorial/ButtonStateChange and am having an issue with almost every switch I try. I’m using several different momentary lever switches, of this style: http://www.superdroidrobots.com/images/TE-044-000.jpg. Whenever I press and hold the switch, it is supposed to not add another count until let go, but it streams hundreds of “presses” in the serial port. This has happened with 3 different momentary switches from different suppliers. What’s going on here? I’ve got one side of the switch to digital pin 2, the other side connected to 5v, and a 10K resistor between 5V and ground.

Edit: also tried it on other pins than digital 2 with same results

I've got one side of the switch to digital pin 2, the other side connected to 5v, and a 10K resistor between 5V and ground.

No that's not how you connect buttons. The resistor must be connected to the input pin and one side of the switch. Then the other end of the resistor and the other end of the switch go to ground and 5V.

Alex, it sounds like switch bounce. This is the code (taken from the Arduino site) I use:

const int DebounceDelay = 150;

void CounterHandler() { static unsigned long LastButtonTime = 0; unsigned long ButtonTime = millis();

if (ButtonTime - LastButtonTime > DebounceDelay) { Counter ++; }

LastButtonTime = ButtonTime; }

This works fine for me.

Z

it sounds like switch bounce

You don't get:-

hundreds of "presses"

with contact bounce. My bet is on a floating input.

arghhh... thanks mike. I always forget which pairs on a tactical switch are connected. All's good now, no bouncing problem. ;) Thanks

For others reading this thread:

Confused by "floating input", "bounce"?

Introduction to connecting switches at...

http://sheepdogguides.com/arduino/aht0button.htm

with contact bounce. My bet is on a floating input.

Having read a bit more on floating inputs, what's the difference between using an external resistor and the Arduino's internal pull up resistor?

Z

Cost. The external resistor adds a few pennies to your project.

Direction. An external resistor can be wired either pull-up or pull-down. The internal resistor is only pull-up.

Right, so is there any advantage to using an external pull up resistor over the Arduino's own?

Z