issue with stepper code works until I want to put if statement in

The following code works until insert:
** lim1_st = digitalRead(lim1);**
** if(lim1_st == LOW);{**
** x = stepsPerRevolution;**
** }**

My intention is have a limit switch stop the stepper motor.
When I comment out just : x = stepsPerRevolution;
It works.

So that tells me for some reason x is getting set to stepsPerRevolution and making it stop.
But the pin stays high the entire time so I don't know why it enters that if statement and makes x = stepsPerRevolution;.

Thank you for your time.

// Define pin connections & motor's steps per revolution
const int dirPin = 10;
const int stepPin = 9;
const int lim1 = 2;
const int lim2 = 3;
const int remote = 1;
const int drvEN = 0;
const int stepsPerRevolution = 400;

int lim1_st = 0; //Button state initial value
int lim2_st = 0; //Button state initial value
int flag1 = 1;  //flag to keep track of button presses
int flag2 = 0;
int remote_st = 0;

void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(lim1, INPUT);
  pinMode(lim2, INPUT);
  pinMode(remote, INPUT);
  pinMode(drvEN, OUTPUT);

  digitalWrite(drvEN, HIGH);
  
}
void loop()
{
  remote_st = digitalRead(remote);
  if(remote_st == LOW) {

    if(flag1 == 1){
      digitalWrite(drvEN,LOW);
  // Set motor direction clockwise
  digitalWrite(dirPin, HIGH);

  // Spin motor slowly
  for(int x = 0; x < stepsPerRevolution; x++)
  {
  lim2_st = digitalRead(lim2);
    if(lim2_st == LOW);{
      x = stepsPerRevolution;
    }
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
   digitalWrite(drvEN,HIGH);
  delay(1000); // Wait a second
  flag1 = 2;
    }
  }
  remote_st = digitalRead(remote);
if(remote_st == LOW) {
  if(flag1 == 2) {
     digitalWrite(drvEN,LOW);
  // Set motor direction counterclockwise
  digitalWrite(dirPin, LOW);

  // Spin motor quickly
  for(int x = 0; x < stepsPerRevolution; x++)
  {
  lim1_st = digitalRead(lim1);
    if(lim1_st == LOW);{
      x = stepsPerRevolution;
    }
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
   digitalWrite(drvEN,HIGH);
  delay(1000); // Wait a second
  flag1 = 1;
}
}
}

If you want to terminate a FOR loop the appropriate instruction is break;

If you don't have pull-up resistors on your limit switch pins they may be giving spurious results. It's better to use
pinMode(pin, INPUT_PULLUP); which sets the internal pull up resistor.

...R

Thank you for the suggestion.

I have pull up resistors on the limit switches.

I can try the internal pull-up. But still confusing why externals wouldn’t work and why the code is entering the if statement when it’s not true.

I’ll look into break. Never used before.
Thanks again!

The following code works until insert:

  lim1_st = digitalRead(lim1);

if(lim1_st == LOW);{
     x = stepsPerRevolution;
   }

There's an error here - the semicolon on the second line.

A lone semicolon is the empty statement which does exactly nothing. So the
whole if statement does nothing at all and x is always set to stepsPerRevolution.

MarkT:
There's an error here - the semicolon on the second line.

Well spotted.

...R

MarkT:
There's an error here - the semicolon on the second line.

A lone semicolon is the empty statement which does exactly nothing. So the
whole if statement does nothing at all and x is always set to stepsPerRevolution.

Well spotted indeed! Thank you