Stepper wont change directions

I have a stepper motor shield with nema 17 steppers. using accellStepper library everything works okay in one direction. i change change direction by shorting the direction pin high/low. im using an esp8266.

Here is a program that i thought should turn the motor left and right using the millis timer and the direction function. it seems like the motor might try to switch directions but it just gets stuck no matter what acceleration speed i use. Any Idea whats wrong?

#include <AccelStepper.h>

// Define some steppers and the pins the will use
// DRIVER (1) is the right driver for the step/dir drivers like A4988 or DRV8825
AccelStepper stepper1(AccelStepper::DRIVER, 4, 12);
AccelStepper stepper2(AccelStepper::DRIVER, 5, 13);
AccelStepper stepper3(AccelStepper::DRIVER, 0, 14);

unsigned long now = 0;
int motorState = 0;
bool motor1Dir = 0;
bool motor2Dir = 0;
bool motor3Dir = 0;
//const byte enable = 8;

void direction(unsigned long pos, int motor, int dir) {
  if (motor == 1) {
    stepper1.moveTo(pos);
  }
  if (motor == 2) {
    stepper2.moveTo(pos);
  }
  if (motor == 3) {
    stepper3.moveTo(pos);
  }
}

void setup()
{
  Serial.begin(115200);
  stepper1.setMaxSpeed(2000.0);
  stepper1.setAcceleration(100.0);
  //stepper1.moveTo(30000000);

  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(100.0);
  // stepper2.moveTo(30000000);

  stepper3.setMaxSpeed(1000.0);
  stepper3.setAcceleration(1000.0);
  //stepper3.moveTo(30000);

}

void loop()
{

  if (millis() - now >= 5000) {
    if (motorState == 0) {
      motor3Dir = 0;
      direction(2000, 3, 0);
      Serial.println("here");
      motorState = 1;
    }
    if (millis() - now >= 10000) {
      if (motorState == 1) {
        Serial.println("here2");
        motor3Dir = 1;
        direction(0, 3, 1);
        motorState = 0;

        now = millis();
      }
    }
  }

//  stepper1.run();
//  stepper2.run();
  if (motor3Dir) {
    digitalWrite(0, HIGH);
  } else {
    digitalWrite(0, LOW);
  }
  stepper3.run();

}

when switching direction pin to HIGH. the motor just seems to vibrate left and right sometimes moves a little but then gets stuck

The moveTo90 function is for absolute coordinates. If you say moveTo(200) the stepper will move to position 200. If you say moveTo(200) again the stepper will not move because it is already there. Use the move() function to move relative to the current position.

You must issue a run() for each motor as often as possible. Usually call run() for each motor every time through the loop() function.

Yes i understand that,

after 5 seconds direction(2000, 3, 0); //move motor3 2000
after 5 more seconds direction(0, 3, 1); //move motor3 back to 0

i call stepper3.run(); in the loop.

my stepper motor kit is this,

im using an esp8266, could this be because the logic is only 3.3v from the esp?

You do not need to mess with the dir pin state. If stepper3 is at position 3000 and you want to move back to 0, just say stepper3.moveTo(0); OR stepper3.move(-3000); Let the library take care of the dir pin.

As far as I know, the DRV8825 work fine at 3.3V.

I have not any documentation on the stepper shield that explains how the jumpers work. current there are no jumpers installed on the board at all.

I see what you mean. But then why does it just move in the same direction over and over?

How is the CNC shield wired to the ESP8266? Photos?

From this photo you shared,
cnc shield Uno pins

pins i connected,
DIR Z
DIR Y
DIR X
STEP Z
STEP Y
STEP X
ENABLE
GND
3V
5V



What happens when you run Robin2's simple stepper code after changing the pin numbers to match your steppers. Do each stepper to test the connections. When you run the code the motor should move one direction then stop and reverse.

And you need to enable the motors in the code or jumper the enable header pins on the CNC shield.

Sorry that is the one jumper i do have installed. i tried the code with motor 3 like this and it didnt do anything, im going to try the other 2,

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing

byte directionPin = 0;
byte stepPin = 14;
int numberOfSteps = 1000;
byte ledPin = 13;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 250; // milliseconds - or try 1000 for slower steps


void setup() {

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);

  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);


  digitalWrite(directionPin, HIGH);
  for (int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
    digitalWrite(stepPin, LOW);

    delay(millisbetweenSteps);

    digitalWrite(ledPin, !digitalRead(ledPin));
  }

  delay(3000);


  digitalWrite(directionPin, LOW);
  for (int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    // delayMicroseconds(pulseWidthMicros); // probably not needed
    digitalWrite(stepPin, LOW);

    delay(millisbetweenSteps);

    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

void loop() {
}

Same for the other motors..?!?!

I did figure out my pins for DIRECTION were on the wrong motors. my code works now. thanks !!

How can i tell the motor move to 1000 THEN move back to 0 without using my millis timer.

How can i tell if position is == or >= ?

  unsigned long distanceToGo = stepper3.distanceToGo();
  if (distanceToGo == 0) {
    if (motor3Dir == 0) {
      direction(1000, 3, 1);
      //  Serial.println("here");
    }
    if (motor3Dir == 1) {
      direction(0, 3, 1);
      // Serial.println("here2");
      motor3Dir = 0;
    }
  }
  if (distanceToGo == 1) {
    //  Serial.println("here3");
    motor3Dir = 1;
  }