button/code issue

I'm having an issue with what I would think was a simple piece of code. I have a break in my code, where I want the program to wait until the user presses a button.

code code code

while (digitalRead(buttonOnePin)) { }

continue code...

The issue I am having is that Arduino just seems to fly right by this, sometimes it waits, sometimes pushing the button doesn't have any effect at all and the code doesn't continue.

I know by enabling serial reading that my button pin is reading zero until i hit the button, so that doesn't seem ot be the issue.

Should I be using a better method for waiting for user input?

Is the button normally low, and takes the pin high when pressed? If so, try

while (!(digitalRead(buttonOnePin))) { }

(I added the ! )

Thanks! I'll try that after this silly teevee debate which seems to be pretty binary in nature as well.


Based on your description, I would guess that the button has been miswired somehow and the pin is floating. My standard button implementation is


pinMode(pin, INPUT);
digitalWrite(pin, HIGH); // enable the pullup resistor

This configuration should go low when the button is pressed and pop back high upon release. In this case your original loop should work fine. I use something similar all the time (or Oracle's variation for buttons wired the other way).


Thanks M, I thought it might have been floating as well, based on the random nature of success. I enabled debugging, though, to make sure the pin was reading LOW until i hit the switch. The serial read was showing 0 until the button was pushed (at which point i read 1).

That being said, i will surely compare my circuit against your recommendation to make sure everything is in order.

Yeah, but it's not inconceivable that it's both floating AND influenced by the button state or perhaps the proximity to your hand. I think the observation that the your digitalRead() loop, which is otherwise perfectly good, fails in every possible way, all but proves that something is amiss with the HW.

If you look at your circuit and are convinced that it and the init code (did you remember to enable that pullup resistor?) are good, just write a little test loop like

while (true)

and watch the results over a long period of time.


A question: Are you debouncing this button?

wow, ended up being a delay issue! Basically, i was hitting the button, but it was just flying through the next few steps while the button was still HIGH.

about 500 ms of delay after the button press to wait for me to take my finger off was the solution.

Thanks everyone for weighing in. I'm really noticing, as I get deeper in to physical computing, that I am having to stick little amounts of delay EVERYWHERE, mostly to compensate for the real world.

In comparison to the real world, these chips are incredibly fast. It looks like the Diecimila I have here has a 16MHz crystal. It's not unreasonable to expect it to complete a single instruction in one microsecond, probably less.

about 500 ms of delay after the button press to wait for me to take my finger off was the solution.

No you just want another loop that waits for the button to be released before you continue your code. With a small delay before that to take care of contact bounce.