Problem with button

Hi guys,
A simple question, i make a digitla clock with arduino , i want to press a button to set time in digital clock, but i have problem with button .Particularly, my button is HIGH without pressing.

code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int const button1=7;
int const button2=8;
int const button3=9;
int biendem1=0;
int biendem2=0;
int biendem3=0;
int h=12;
int m=12;
int s=12;
int TIME;
int buttonStatus1=0;
int buttonStatus2=0;
int buttonStatus3=0;
void setup() {
lcd.begin(16, 2);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
Serial.begin(9600);
}
void loop() {
int buttonStatus1=digitalRead(button1);
int buttonStatus2=digitalRead(button2);
int buttonStatus3=digitalRead(button3);
if (buttonStatus1==HIGH)
biendem1++;
if (buttonStatus2==HIGH)
biendem2++;

if (biendem1%2!=0){
delay(1000);
lcd.setCursor(0,0);

lcd.print(h);
lcd.print(":");
lcd.print(m);
lcd.print(":");
lcd.print(s);
if (biendem2==1){
lcd.setCursor(0,1);
lcd.print("set hour");
if (buttonStatus3==HIGH)
h++;
if (h==24)
h=0;
}
if (biendem2==2){
lcd.setCursor(0,1);
lcd.print("set minute");
if (buttonStatus3==HIGH)
m++;
if (m==60)
m=0;
}
if (biendem2==3){
lcd.setCursor(0,1);
lcd.print("set second");
if(buttonStatus3==HIGH)
s++;
if (s==60)
s=0;
}
}
else{
lcd.setCursor(0,0);
s=s+1;
lcd.print("TIME: ");
lcd.print(h);
lcd.print(":");
lcd.print(m);
lcd.print(":");
lcd.print(s);
delay(1000);
lcd.clear();
if(s==60){
s=0;
m=m+1;
}
if (m==60)
{m=0;
h=h+1;
}
if (h==24)
{h=0;
}
lcd.setCursor(0,1);
lcd.print("First Project");
}
Serial.println(biendem1);
}

Thanks

input pin 'floats' if it isn't connected to anything. so you read a random value 0 or 1.

you must decide how do you want to connect the button.

Tutorial/InputPullupSerial
Tutorial/Button

i am newbie so you can explain it to me please!! ,
sr, i dont know how to upload photo . i design button like that

thanks

To avoid this kind of fluctuations when working with buttons it´s useful to work with "pull-up" or "pull-down".Read about these concepts, take a look at the links provided above by the user Juraj. There you have a solution to your problem. I´m newbie too and it´s simple to understand.

In this case simply connect your buttons like this:

and declare the inputs like this:

pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPU_PULLUPT);
pinMode(button3, INPUT_PULLUP);

It´s the simplest way to connect a button without any resistance.

That diagram looks wrong to me.

Isn't that pulldown on the wrong side of the button? Or is there some way to turn the button before pushing into the breadboard so it will be wrong? It looks like it pulls the power side of the circuit down, leaving a charged wire to pin 2 when the button is released, no longer connected to the pulldown. It would also drain current all the time, not just during button press.

@LiebertTruong

Better is to use the pin mode INPUT_PULLUP on the button sensing pin (pin 2). It will provide weak current to the pin through a resistor in the chip (saves you needing to wire in a resistor) that simply by grounding will make the pin LOW and not grounded makes the pin HIGH -- opposite to the HIGH and LOW when providing the pin voltage for pin mode INPUT. You wire the sense pin to the button and then other side of button directly to ground/ The circuit works safely with very little waste and uses less parts (a wire and resistor less) and connecting.

Pin HIGH and LOW are only electrical states. If the button is pressed to make the pin LOW and HIGH when not pressed then it can be detected is all you need. Just make sure that the code knows which is pressed and no pressed, LOW and HIGH.

How much do you know of electricity? A wire that has charge in it, even unconnected has an internal electrical pressure.

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.

If you are using one of those 4 pin tactile buttons you may have it in the protoboard the wrong way, lift it out and turn it 1/4 turn or 90Deg and refit it.

Tom... :slight_smile: