Buttons Auto Pressed/Not working

Hey, my problem is that my buttons don't work. If I pull them out from arduino digital input, the code triggers the buttons automatically. If I try to press them the buttons don't respond.

const int b1incrtime = 13;
const int b2decrtime = 10;
const int b3incrlevel = 9;
const int b4decrlevel = 8;

int button1State = 0;
int button2State = 0;
int button3State = 0;
int button4State = 0;

 pinMode(b1incrtime, INPUT);
  pinMode(b2decrtime, INPUT);
  pinMode(b3incrlevel, INPUT);
  pinMode(b4decrlevel, INPUT);

  button1State = digitalRead(b1incrtime);
  button2State = digitalRead(b2decrtime);
  button3State = digitalRead(b3incrlevel);
  button4State = digitalRead(b4decrlevel);
if (button1State == HIGH & water > 100 )
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Water Time");
    lcd.setCursor(0, 1);
    watertime = watertime + 1;
    lcd.print(watertime); lcd.print(" "); lcd.print("seconds");
    delay(200);

  }


  if (button2State == HIGH & water > 100 )
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set Water Time");
    lcd.setCursor(0, 1);
    watertime = watertime - 1;
    lcd.print(watertime); lcd.print(" "); lcd.print("seconds");
    delay(200);

  }


  if (button3State == HIGH & water > 100 )
  {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set When");
    lcd.setCursor(0, 1);
    lcd.print("Irigation Start");
    lcd.setCursor(0, 2);
    moisturelevel = moisturelevel + 1;
    lcd.print(moisturelevel); lcd.print(" "); lcd.print("Moisture");
    delay(200);

  }


  if (button4State == HIGH & water > 100 )
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Set When");
    lcd.setCursor(0, 1);
    lcd.print("Irigation Start");
    lcd.setCursor(0, 2);
    moisturelevel = moisturelevel - 1;
    lcd.print(moisturelevel); lcd.print(" "); lcd.print("Moisture");
    delay(200);

  }

If I set the buttons state to LOW, they auto trigger themselves again.

Because the code you posted doesn't compile?

Hi,
Can I suggest you write some code that JUST tests your buttons, no other code or hardware connected.
This will be the simplest and basic test.

Can you please post some images of your project?
So we can see your component layout.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Why not use the built in pullup resistors activated by pinMode(pinNr, INPUT_PULLUP);

How do you do that?

Given your schematic they should read as LOW if you have not pressed them.

Maybe you have the pin numbers wrong. Those four pin push switches should be wired across the diagonal. That is use the pins on opposite corners. Either corners will do.

Maybe you remember my last week post where you helped me with some suggestions about how should I change the connection in my circuit. I will try to test the buttons with a simple code.

It doesn't compile cause the code is a little long and I didn't post all of it :smiley:

I tried to wire them across the diagonal to test it too, and still they don't respond if I press them. I will try to rewire them and test them again.

I never tried that, the thing is these buttons used to work, and now don't, maybe there is a loose wire or something that I don't see. I will try this to use the built in pullup resistor and see how it works.

Then assuming you are using solderless bread board, then rotate the push button through 90˚. Or place them so that they span the gap between the top and bottom set of connectors.

I use wires soldered to the buttons pins, then the wires come to 5V, GND and Digital Input. And yes, I am using solderless breadboard :smiley: .

Best try what I said, and if you get no joy then send a photograph of how you wire them.

I will try all the advices tomorrow and see what works, and what doesn't. Thank you!

Update, using Input_PULLUP actually works and buttons are working.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.