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.
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.
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() {
}