# stopping stepper motor using limit switch

Ihave my stepper motor NEMA 23 400 step/rev
24V power supply
Wanta DQ542MA 2H Mirostep driver
2 limitswitch
arduino uno

this is the scenario:

when my stepper motor reach the first limit switch it will rotate clockwise

my problem is how can i stop the motor or the program when it reaches the other limit switch?

this is my code:

#include <Stepper.h>

const int stepsPerRevolution = 6000; // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

int stepCount = 0; // number of steps the motor has taken
int limit = 7;
int limit2 =6;
int limitstate = 0;
int limitstate2 =0;
int x = 0;
int y = 0;
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
int halt = A0;
pinMode(limit, INPUT);
pinMode(limit2, OUTPUT);
}

void loop() {

int limitstate= digitalRead(limit);
Serial.print("x is: “);
Serial.print(x);
Serial.print(” Limit State is: ");
Serial.println(limitstate);

int limitstate2= digitalRead(limit2);
Serial.print(“y is:”);
Serial.print(y);
Serial.print(" Limit State is:");
Serial.println(limitstate2);
myStepper.step(1);
Serial.print(“steps:”);
Serial.println(stepCount);
stepCount++;

if (x == 0 && y == 0)
{
if (limitstate == 0){
// step one revolution in the other direction:
Serial.println(“counterclockwise”);
myStepper.step(24000);
}
else
{
x++;
}
}
else if (x == 1)
{
// step one revolution in one direction:
Serial.println(“clockwise”);
myStepper.step(-24000);

if (limitstate2 == 1){
exit (0);
}
}
/*
*
else
{
y++;
}
if (y == 1)
{
exit (0);
}

*/
}

pls help me, this is for my thesis project thank you.

The same way you did for the first one… check if you hit the switch…

Please correct your post above and add code tags around your code:
`[code]`` [color=blue]// your code is here[/color] ``[/code]`.

It should look like this:`// your code is here`
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

I presume your stepper driver takes step and direction signals? if so the standard Stepper library is not really intended for that. Have a look at the AccelStepper library and its DRIVER option. Or just write your own code as in this Simple Stepper Code

If you want to detect a limit switch you MUST use non-blocking stepper code so that the switch can be checked between steps. The AccelStepper stepper.run() and runSpeed() functions are non-blocking.

...R