How to fix digitalRead

So my problem is that after like two readings of digital pin 6,7 the pin stays on forever for some reason here’s my code. I can’t seem to find why does it happen I tried to add some delay(x); but it didn’t help … never happend for pin 5 which is even more strange to me… any help will be appreciated
my buttons pin are set to INPUT_PULLUP that’s why Im checking for low

#include <Stepper.h>

int obstaclePin = A0;
int obstaclePin2 = A1;
int obstaclePin3 = A2;

const int button1Pin = 5;
const int button2Pin = 6;
const int button3Pin = 7; 

const int stepsPerRevolution = 80;

Stepper myStepper(stepsPerRevolution, 50, 51, 52, 53);
Stepper myStepper2(stepsPerRevolution, 46, 47, 48, 49);
Stepper myStepper3(stepsPerRevolution, 42, 43, 44, 45);

void setup() {
  myStepper.setSpeed(100);
  myStepper2.setSpeed(100);
  myStepper3.setSpeed(100);
  
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  
  pinMode(obstaclePin, INPUT);    // sets the sensor pin
  pinMode(obstaclePin2, INPUT);    // sets the sensor pin
  pinMode(obstaclePin3, INPUT);    // sets the sensor pin
  
  pinMode(button1Pin, INPUT_PULLUP);    // declare pushbutton as input
  pinMode(button2Pin, INPUT_PULLUP);
  pinMode(button3Pin, INPUT_PULLUP);

  Serial.begin(9600);    // initialize the serial port:
}
int counter = 0;
void loop() {
  while(true){
    if(digitalRead(button1Pin)==0){
      counter = 1;
      break;
    }
    else if(digitalRead(button2Pin)==0){
      counter = 2;
      break;
    }
    else if(digitalRead(button3Pin)==0){
      counter = 3;
      break;
    }
  }
  while(counter!=0){
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
//*******************************************************************************************************************************************************
    if(counter==1){
      Serial.println("Motor 1");
      myStepper.step(1);
      if(analogRead(obstaclePin)<=400){
        counter=0;
        break;
      }
    }
//*******************************************************************************************************************************************************
    else if(counter==2){
      Serial.println("Motor 2");
      myStepper2.step(1);
      if(analogRead(obstaclePin2)<=400){
        counter=0;
        break;
      }
    }
//*******************************************************************************************************************************************************
    else if(counter==3){
      Serial.println("Motor 3");
      myStepper3.step(1);
      if(digitalRead(obstaclePin3)==0){
        counter=0;
        break;
      }
    }
  }
}

Thanks for using code tags, but you failed to post all your code.

Alright now you can see all of it

Odd code, using "break" in if statements. "Break" is normaly used in swtich/case environment. What school tought You that?

Railroader:
Odd code, using "break" in if statements. "Break" is normaly used in swtich/case environment. What school tought You that?

The "if"s are inside a "while(true)"

It breaks the while loop .. added it just to make sure thought counter=0 isn't enough

What do you mean by "the pin stays on forever"? Which pin? What do you mean by "on"?

I recommend that you add more Serial.println() statements to your code and then run it with the Serial Monitor open to get a better idea of what it happening.

Of coures I did that everything works fine but after like 3-4 tries the button on pin number 6,7 says that the button is pressed even when its not..

MartinLukasMikus:
So my problem is that after like two readings of digital pin 6,7 the pin stays on forever

Is it the pin or what happens when count is the number 1, 2, or 3 in the bottom of your code?

You have a while (counter != 0) and only set the counter to zero under certain conditions.

You also do zero button debouncing. Is this because you have caps across the button contacts? (hardware debounce)

MartinLukasMikus:
Of coures I did that everything works fine but after like 3-4 tries the button on pin number 6,7 says that the button is pressed even when its not..

Where's your project's schematic? That's another major suspect in this case.

Wiring errors as well, including poor contacts.

Bad buttons is yet another option.

Also those while() loops in your code are just plain weird. You normally don't need that kind of constructions as loop() is doing just that.

It is odd that you have two obstaclePins that give an analog output, one that gives a digital output. Different sensors?

GoForSmoke:
You also do zero button debouncing. Is this because you have caps across the button contacts? (hardware debounce)

The second while() loop that the program gets stuck into after the first button press should take care of any bounce.

If a person with slow code doesn't debounce a project then if/when their code gets fast they can believe that whatever they did broke the sketch and back off to jerk and stutter code not realizing that one problem being fixed opened up the next one needing to be fixed.

What if 2 buttons are pressed together? Code should cover the things you're not supposed to do too.

It's not really clear what this code is supposed to accomplish but it appears to me that a button press starts some movement. That's a situation where debounce is not needed in the first place, as only the first press matters.

The way it's written it's impossible to have two presses at the same time, as the moment one press is detected the button checking stops and the movement starts. Until that movement is completed (based on the reading of an obstacle sensor), there is no more checking for button presses.

Even though it's poorly written it seems that the basic behaviour of the software is as OP intends, except that after a while phantom button presses appear. That's at least my understanding.

Pin 6, if the obstacle read is > 400 throughout the stepper movement it doesn’t exit and can’t read anything else.

    else if(counter==2){
      Serial.println("Motor 2");
      myStepper2.step(1);
      if(analogRead(obstaclePin2)<=400){
        counter=0;
        break;
      }
    }

My #1 suggestion is see what’s really happening.

  while(true){
    if(digitalRead(button1Pin)==0){
      Serial.println( F( "button 1 pressed" ));
      counter = 1;
      break;
    }
    else if(digitalRead(button2Pin)==0){
      Serial.println( F( "button 2 pressed" ));
      counter = 2;
      break;
    }
    else if(digitalRead(button3Pin)==0){
      Serial.println( F( "button 3 pressed" ));
      counter = 3;
      break;
    }
  }

wvmarle:
It's not really clear what this code is supposed to accomplish but it appears to me that a button press starts some movement. That's a situation where debounce is not needed in the first place, as only the first press matters.

The way it's written it's impossible to have two presses at the same time, as the moment one press is detected the button checking stops and the movement starts. Until that movement is completed (based on the reading of an obstacle sensor), there is no more checking for button presses.

Even though it's poorly written it seems that the basic behaviour of the software is as OP intends, except that after a while phantom button presses appear. That's at least my understanding.

That's exactly what is happening and I really don't know why maybe it's because I'm not using original arduino or maybe I should add some resistor to the button, but I thought when using INPUT_PULLUP no resistors are needed

MartinLukasMikus:
That's exactly what is happening and I really don't know why maybe it's because I'm not using original arduino or maybe I should add some resistor to the button, but I thought when using INPUT_PULLUP no resistors are needed

You don't need resistors with INPUT_PULLUP because the chip supplies 5V through 20K to 50K ohms to the "weakly pulled up" pin.

Debounce is not about that. It's about tiny sparks jumping the gap when the contacts are very close. When your code is watching the button 10's of times per millisecond you see a short series of press and release events for every change. So you can put a capacitor across the button contacts to eat the sparks or you monitor the pin while doing everything else, no waiting around for no change in 2 to 5ms (or longer, maybe 20ms for very 'dirty' wobble-contact buttons) and call that stable. There are many methods, some people just time 20ms to 50ms after the first pin state change detect and look again then, again without blocking the rest of the sketch from running.

The difference is code that checks, comes back later to check again and code that doesn't come back later but just sits and watches the one pin while wasting 16000 cycles every millisecond of delay. And by come back later I mean in a few microseconds, over and over just determining the last stable button state and updating a variable with that.
Other parts of the sketch only read the stable state and act on it, that code can be cleaner without all those details.

MartinLukasMikus:
That's exactly what is happening and I really don't know why maybe it's because I'm not using original arduino or maybe I should add some resistor to the button, but I thought when using INPUT_PULLUP no resistors are needed

And that's why we always ask for your schematics - to find that kind of issues. But you still didn't post it.

Maybe the internal pullups are too weak? Too much noise? Schematics often give clues to that.

Images of your actual setup could be useful, too.