Duemilanove input problem

Hey all, first post here, so be easy.

I got an arduino recently and I have been playing around with it, mostly with LEDs and a nunchuck since I don't really have anything else.

Anyways, I was playing around with getting the LEDs to run a pattern while a button in port 2 is pressed.

here's the code.

#define cPin 13
#define aPin 12
#define downPin 11

#define buttonPin 2

void setup()
  pinMode(cPin, OUTPUT);
  pinMode(aPin, OUTPUT);
  pinMode(downPin, OUTPUT);
  pinMode(buttonPin, INPUT);

void loop()
  if (digitalRead(buttonPin) == HIGH){
    digitalWrite(downPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(cPin, LOW);
    digitalWrite(cPin, HIGH);
    digitalWrite(cPin, LOW);
    digitalWrite(aPin, HIGH);
    digitalWrite(aPin, LOW);
    digitalWrite(downPin, LOW);
    // Done

an LED is popped into 11 12 and 13, then each put in series with their own 1k resistor, then shoved into the ground rail. pin 2 goes to one side of the switch that came with my kit, and the second side goes to ground.

Compiled and loaded, and it's running like the buttons pressed. When I press the button it stops, so it looks like the pin defaults to high (some how)

I switched the if to check for low, and connected the other side of the switch to both 5v and ground. but now when I even TOUCH the wire (insulated part) with my FINGER the LEDs light up. When I press the button, the light up, and keep at it for about half a second after I let go. What's going on?


ok it looks like I need a pull down resister (kit didn’t come with anything high though :s)

any idea why touching the wire worked though?

You can make you sketch work using the internal pull up as follows:

Add the following to setup: digitalWrite(buttonPin, HIGH);

This will work if you change

digitalRead(buttonPin) == HIGH To digitalRead(buttonPin) == LOW

It may help to think of LOW as the button being pressed, HIGH as the button being released.

Have fun!

Oh neat. Can I do the opposite, write LOW, then continue to check for high?

You can't do the opposite - Writing HIGH on an input pin turns pull-ups on, Writing LOW disables pulls-ups. If you want the pin to be HIGH when pushed you have to use external pull-down resistors and I don't think its worth the trouble to make that change.

OK so there isn't any internal pulldowns. That's pretty convenient though, thanks :)

Any idea why touching the wire (with my finger) before would hold it at high?

It's been explained in many many threads: a disconnected pin can be affected by any and all manner of influences, including nearby moisture, the phase of the moon, and the dining preferences of your local mayor. If you want real input data, then really connect the input pin as part of a circuit.

Instead of having the switch(when pressed) apply a high voltage (which I'm assuming you are using the onboard 5v supply?)

directly connect your HIGH voltage supply to pin 2, with a resistor, and have the switch(when pressed) ground pin 2. This avoids the floating pin caused by a disconnected switch, and avoids WRITING to an input pin.