hey everyone!
i make a project that include button.
when i try to get the value from the button its 0(before i pressed on the button).
after i pressed its change to 1, and jumping from 1 to 0 all the time.
i try to disconnect the button and test it only with the wire' its still unstable..
what the problem ? why i do not get only 1 when its pressed and 0 when is not..?
Because your input pin is floating.
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
The following diagram could be helpful to connect a switch/button with an input-line of the Arduino UNO:
To avoid floating condition of the switch/button as remarked in Post#1, the switch/button/key should be connected either with an external pull-up resistor or an external pull-down resistor depending on the requirement of the program logic. The internal pull-up did not work very well for me as a pull-up resistor for a switch/button and interrupt pin.
the easiest way to do a button is to connect one end to your arduino pin and the other to ground. then:
void setup(){
pinMode(Button, INPUT_PULLUP);
}
void loop(){
if (digitalRead(Button, LOW)){
delay(500); // de-bounce button to prevent multiple rapid readings
[i]do something...[/i]
}
}
GolamMostafa:
... The internal pull-up did not work very well for me as a pull-up resistor for a switch/button and interrupt pin.
INPUT_PULLUP
has always worked perfectly for me. It makes wiring switched so easy!
GolamMostafa:
The following diagram could be helpful to connect a switch/button with an input-line of the Arduino UNO:
No it is not.
It shows the input pin as floating which is exactly what you don’t want to do.
Do better.
A diagram should clarify, rather than obfuscate the connections.
GolamMostafa's circuit is at the top, and Mike's simpler, yet functionally equivalent, circuit is the bottom one in the diagram below:
- use INPUT_PULLUP, by connecting
pin
Button
- use a debounce (hw or sw)
- use a debounce (hw or sw)
There is a lot of knee jerk nonsense about debouncing of inputs . It depends on what the button is doing whether you need to debounce it or not, most if the time you don’t.