Control a NEMA 17 stepper motor withTB6560 driver and 2 limit switches

Hi everyone
With the following materials :-
Arduino UNO
TB6560 motor driver
NEMA 17 bipolar stepper motor
2 limit switches (NO pin, NC pin and Ground pin(C))
power supply of 12V (SMPS)

I am trying to rotate the stepper motor. the problem statement is as follows:-
When power supply is given, the motor starts rotating in a specified direction. As soon as it touches the LIMIT SWITCH 1 and reverses its direction and moves in other direction. Again as soon as it touches LIMIT SWITCH 2 it reverses its direction and so on.
Also the speed needs to be controlled.

I tried with few codes but its not working properly. Sometimes complicated errors are arising.
PIN Configuration:-

STEP PIN (CLK+)---PIN 5 of arduino
DIR PIN(CW+)----PIN 2 of arduino
ENABLE PIN(EN+)----PIN 8 of arduino
CLK-,CW-,EN- -----> GND of arduino

Can someone please help me out with a proper code where we are using 2 limit switches to control NEMA 17 ?. Its urgent

Robin2's simple stepper code.

Take a step, check the switch, if the switch is still open take a step and check the switch. Keep doing that until the switch is closed. When the switch is closed change the state of the dir pin and start again. Take a step, check the other switch and so on. Adjust the delay between steps to adjust the speed.

If you want help with the errors, post the error (the complete error message) and the code. Read the how to use this forum-please read stickies to see how to properly post code and error messages.

How to use steps in the code? I am new to this so can you please show me the part of the function like how to use it inside code?

The code that i was using till now is :-
const int stepPin=5;
const int dirPin=2;
const int enPin=8;
const int limitPin=7;
void setup(){
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(limitPin,INPUT);
pinMode(enPin,OUTPUT);
pinMode(enPin,LOW);
}
void loop(){
int limit= digitalRead(limitPin);
if(limit==LOW){
digitalWrite(dirPin,HIGH);
for(int x=0;x<3500;x++){
digitalWrite(stepPin,HIGH);
delay(500);
digitalWrite(stepPin,LOW);
delay(500);
}
}
else if(limit==HIGH){
digitalWrite(dirPin,LOW);
for(int x=0;x<3500;x++){
digitalWrite(stepPin,HIGH);
delay(500);
digitalWrite(stepPin,LOW);
delay(500);
}
}
}
but this was with 1 limit switch.. I want with 2 limit switches. so please help me out

goalcoast:
but this was with 1 limit switch.. I want with 2 limit switches. so please help me out

Add the extra code for the other limit switch. Then choose which switch should be checked based on the direction setting.

...R