Pin doesnt read button change

Hi everyone, I'm not really sure where to post my problem, this is my very first post here, sorry if this isn't the best place.

I'm making a system with an Arduino UNO, an A4988 driver and a stepper motor, i've been doing tests and everything seems to work fine, I added a potentiometer to control the speed and it also works fine.
But when i'm adding 3 pushbuttons: one to make the motor run left, another one to make it run right and the last one to make it stop, somethings not working now.
When I'm pressing the button to run right and to stop works fine, but the other one doesn't work at all.
The code seems to be fine, and I added some lines to check what was going on, and watching at the serial monitor, the digitalRead of the left button doesn't change. I've tried different switches, different cables and nothing, I got a LED attach to the output of the switch and it shines when I press the button, but the pin doesn't get that change.

This is how I got it connected (sorry for the crappy drawning):


-----5V----| |
| |
| button |
---10k--|--| |-------LED
| |______|
|
digitalPIN

I've attached an image with the whole thing.

Any ideas anyone?

The code seems to be fine

That's nice.
Need a second opinion?

Here's the code, I know it's not perfect but i don't think the error comes from here, since the other 2 buttons are working fine.

int steps = 2;
int direccion = 3;
int reset = 4;
int valorPotenciometro = 0;
int velocidad = 0;
int botonIzquierda = 11;
int botonDerecha = 10;
int botonStop = 9;

void setup() {                
 pinMode(steps, OUTPUT); 
 pinMode(direccion, OUTPUT); 
 pinMode(reset, OUTPUT);
 pinMode(botonIzquierda, INPUT);
 pinMode(botonDerecha, INPUT);
 pinMode(botonStop, INPUT);
}

void loop() {
 digitalWrite(reset, LOW);
 delay(100);

 if (digitalRead(botonIzquierda) == HIGH) {
   digitalWrite(reset, HIGH);
   digitalWrite(direccion, LOW);
   
   while (digitalRead(botonStop) == LOW && digitalRead(botonDerecha) == LOW) 
  {
     valorPotenciometro = analogRead(A0);
     velocidad = map(valorPotenciometro, 0, 1023, 1, 10);
     digitalWrite(steps, HIGH);
     delay(velocidad);
     digitalWrite(steps, LOW);
     delay(velocidad);
  }
 }

 else if (digitalRead(botonDerecha) == HIGH) {
   digitalWrite(reset, HIGH);
   digitalWrite(direccion, HIGH);
   
   while (digitalRead(botonStop) == LOW && digitalRead(botonIzquierda) == LOW) 
   {
     valorPotenciometro = analogRead(A0);
     velocidad = map(valorPotenciometro, 0, 1023, 1, 10);
     digitalWrite(steps, HIGH);
     delay(velocidad);
     digitalWrite(steps, LOW);
     delay(velocidad);
   }
 }
 
 else if (digitalRead(botonStop) == HIGH) {
   digitalWrite(reset, LOW);
 }
}

Moderator edit: Code tags corrected

I'm not sure I understand your ASCII art.
Could you maybe sketch your circuit on paper and post a photo instead?

Here it goes. All three buttons are conected to power (5V) at one side, and a 10k ohm resistor (which goes to ground) and an input pin of the arduino. But only two of them seems to work.

The button 4 pins have connection like this

How you connect your 5V, 10K Resistor, and LED? to which pin on the switch 1, 2, 3, and 4.

Hi, the connections in the switches are correct i think
PIN 1 = 5V
PIN 2 = nothing
PIN 3 = 10k ohm resistor + wire to an input in the Arduino
PIN 4 = LED

All the three switches are connected the same, and two of them works. In the other one I get the LED lighning when I push the button, but no signal gets to the Arduino (it's supposed to change from 0 to 1 when I pushed it).

You need to have resistor 330 ohms in series with the LED.
Without resistor, the LED will pull the pin to a voltage that still read as "0".

I've just tried it and nothing. I've tried with other resistors and removing all the LEDs. Still nothing. What I don't understand is that one of the buttons, the one to run the motor in one direction works perfectly, but the other one, with the same code, the same connections, doesn't work and I don't get any signal in the PIN where is connected :frowning: I've tried other PINs and it doesn't matter, so I don't know what else can I try.

Are you sure that the switch actually works and shorts the connections that you expect when you press it ? If you move all 3 buttons to different places in the circuit does the problem follow the button or stay with the pin ?

Yes, I've tried moving the button in different places of the breadboard and still the same issue. I've added the LEDs to check out that the button actually did what they were supposed to, and when I pressed the buttons the LEDs light.

I've also tried, right now, changing the the pull down connections that I had into a pull up connections, but it doesnt solve the problem. When I pressed that button the input doesn't change

Ok, solved, I guess something must be wrong with the switch, even I've tried a couple of them, but using a cable directly instead of a switch works fine. Maybe the breadboard doesn't make good connection.

Anyway, thanks for answering everyone :slight_smile: