A question about getting a absolute read out from a 2 pin button.

I have a question about getting a absolute read out from a 2 pin button.

I am running this simple digital readout code :

void setup() {
Serial.begin(9600);
pinMode(2, INPUT);
}
void loop() {
int sensorValue = digitalRead(2);
Serial.println(sensorValue, DEC);
}

Using a 4 pin button I can get a 0/1 reading when I press/release the button.

But when I use a 2 pin button I get 1 when I press it but random 0 or 1 when I release it.
Can someone please explain how can I get a absolute 0 or 1 when I press and release the 2 pin button ?

I have included a labeled diagram of my breadboard.

http://picasaweb.google.com/lh/photo/w-Rm5JeUXBpVfSL_OmYUqg?feat=directlink

ps. how does one insert images into the text of these messages.

But when I use a 2 pin button I get 1 when I press it but random 0 or 1 when I release it.

It's because you have the buttons wired differently. That particular 4 terminal push button has 4 terminals (the vertical pins are connected together internally and the switch pins are left to right). So referencing your 4 pin configuration, you have Digital Pin 2 tied to +5V through the resistor when the button is open and Pin 2 shorted to ground when the button is pressed. In the 2 pin configuration you have Digital Pin 2 floating (not connected to anything) when the button is open and when the button is pressed, tied to +5V through the resistor.

What you want to do is move the Digital Pin 2 wire to the red wire on the switch and connect the black switch wire to ground. This will give you the same results as the 4 pin configuration.

@ Wayneft.
Thank you so much for explaining that to me. I have wrapped my brain around it and now it makes sense, thank you.

J.

To insert images, one way is to put them on the internet somewhere as a .jpg file.
Then press the little image button above (2nd row, 2nd from left) and paste the url between
'bracket img bracket' and 'bracket /img bracket' that comes up.

Another way is to click Additional Options below and browse to the file you want to attach.
I don't know if files besides .jpg will display.