arduino -> nema34 stepper -> 2 limit switches

Hello Everyone,
I have nema34 motor connected to arduino and two limit switches attached, so, motor should run in one direction until it hits limit switch, then switch direction and run backward until another limit switch is touched.
schematics:

Arduino pin3 → stepper driver PUL+
Arduino GND → stepper driver PUL-
Jumper from stepper driver PUL+ to stepper driver DIR+
Arduino pin2 → stepper driver DIR-
lswitch1-COM → pin9 → 5V
lswitch1-NO → lswitch2-COM
lswitch2-NC → GND

and here is my code:

#include <AccelStepper.h>

int ls1 = 9;
const int p2 = 2;
const int p3 = 3;
float spd = 100;

AccelStepper st1(1,3,2);

void setup() {
st1.setMaxSpeed(300);
st1.setSpeed(spd);
pinMode(ls1, INPUT_PULLUP);
}
void loop() {

if ( (digitalRead(ls1) == HIGH))
{
st1.setSpeed(-spd);
st1.runSpeed();
}
st1.runSpeed();
}

it compiles fine. motors runs. touches limit switch and stops. it doesn’t go backward.

Can someone, please, tell me what is wrong with my connections or coding?

Thank you very much in advance!

Use code tags next time. You have now been warned.

There is nothing obviously wrong with that code but without code tags, the forum software may have eaten something crucial.

Look at your limit switch wiring. You have one pin connected to 5 volts, when the pin is already at 5 volts because you have the internal resistor turned on. Best to make up a real wiring diagram(schematic).

Paul

Hi MorganS.
no, it didn’t. code looks ok to me in my browser - like I wrote it. :slight_smile:

Thanks.

I don't see how something wired as you describe could work, post a wiring diagram.