I am attempting to program a bipolar stepper motor with direction switch.

Hello everyone.
I am attempting to program a standa stage which uses a bipolar stepper motor. The motor must change direction once it reaches the end of the stage, the end is indicated by a push button. However it doesn't change direction. Could you please verify whether or not my code is correct?

#include <Stepper.h>
#define BUTTON1 4
#define BUTTON2 5
#define BUZZER 1
#define LED 7
 

 
 int moveleft = 0;
 int moveright = 1;

 const int stepsPerRevolution = 48;  // change this to fit the number of steps per revolution
                          // for your motor
 int maxsteps = 250000;
 
 // initialize the stepper library on the motor shield
 Stepper myStepper(stepsPerRevolution, 12,13);     
 
 // give the motor control pins names:
 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;
 
 int x = 0;
void setup() {
 Serial.begin(9600);
 // set the PWM and brake pins so that the direction pins  // can be used to control the motor:
 pinMode(BUTTON1, INPUT);
 pinMode(BUTTON2, INPUT);
pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
 pinMode(LED, OUTPUT);
 
 // initialize the serial port:
 Serial.begin(9600);
 
 // set the motor speed (for multiple steps only):
 myStepper.setSpeed(200);
 }

 
 void loop() {
   
   myStepper.step(48);
   delay(20);
   if (digitalRead(BUTTON1)){
     digitalWrite(LED, HIGH);
   }
 
   
     while (moveleft, HIGH){
       myStepper.step(48);
       delay(20);
       if (digitalRead(BUTTON1) && !digitalRead(BUTTON2)){
     moveleft = HIGH;
     moveright = LOW;
   }
       }
       while (moveright, HIGH){
         myStepper.step(-48);
         delay(20);
         if (digitalRead(BUTTON2) && !digitalRead(BUTTON2)){
     moveright = HIGH;
     moveleft = LOW;
   }

         
   }
 }

note that the buzzer isn't currently in use. The only other thing that could explain this problem is that the buttons are broken.

The only other thing that could explain this problem is that the buttons are broken.

Or incorrectly wired. I'm leaning towards incorrectly wired. How are they wired?

You are not using the internal pullup resistor. Why not?

Not using it means that you need to have an external pullup or pulldown resistor. Do you?

 int moveleft = 0;
 int moveright = 1;

This is silly. You can only move left or right. It is meaningless to be able to move both directions at the same time. That being the case, you should have a boolean variable, movingLeft that is valued true (when moving left) or false (when moving right), not two variables.

 int maxsteps = 250000;

Perhaps you should take a peak at the values that fit in an int. Hint: This one doesn't.

     while (moveleft, HIGH){

No clue what you are trying to do here, but this isn't right. The comma operator is distinctly different from the == operator.

       if (digitalRead(BUTTON1) && !digitalRead(BUTTON2)){
     moveleft = HIGH;
     moveright = LOW;
   }

The digitalRead() function does not return true or false. You should use explicit, not implicit comparisons here. Primarily because when you wire the switches the easy way, LOW (0) means pressed.

Firstly thank you for replying. Secondly I forgot that the max steps was in there. It is not being used and I just chose a random value for it. I don't know whether the motor needs a pullup or pulldown resistor and I am currently leaning towards the switches being broken too. I don't think that they ever switch off. And I like the new programming ideas that you have given me. So with this comes an overall Thank you.

It is not the motor that needs a pull up resistor but the switch.
Just write a simple sketch that lights an LED when it detects the switch and push it by hand.
Asking again, how is your switch wired. It should be between the digital input and ground, with a resistor between the digital input and +5V, or enable the internal pull up resistors.

there are 2 switches with a common GND/Vin. so there are 3 wires. I have written a script for an LED but the light is constantly on, which is why I think that the switch may be broken. I will attempt your wiring suggestions.