Accelstepper does not working

Hi everyone,

I am designing dosing control system with 5 Stepper motors. So, Hardwares that I used are Pololu 2267 (200Steps/rev, 2.8V 1.7A Phase) Stepper Motor , Pololu A4988 Stepper driver and Ramps 1.6 and 12V 40A PSU.

I connected one stepper motor to the Ramps and it works successfully when I tried with different script rather than using of Accelstepper library.

First, I am sharing the first code that works for me ;

byte directionPin = 55;
byte stepPin = 54;
int numberOfSteps = 2000;
byte ledPin = 38;
int pulseWidthMicros = 500;  // microseconds



void setup()
{

  Serial.begin(9600);

 
  delay(2000);

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

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

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

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(pulseWidthMicros);
   
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
 
}

void loop()
{

}

After this step, I tried different codes with using Accelstepper library, even so using of setMinPulseWidth, motor do not move.
Codes with Accelstepper library are ;

#include <AccelStepper.h>
AccelStepper stepperA(1,54, 55);
int steps = 10000;
int X = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Test");
 if (X < 2)
    {
        stepperA.move(steps);
        stepperA.setSpeed(10000);
        stepperA.runSpeedToPosition();
        delay(2000);
        X = X + 1;
    }

}

void loop() {
  // put your main code here, to run repeatedly:

}
#include <Arduino.h>
#include <AccelStepper.h>


long receivedStep = 0;
long receivedSpeed = 0;
long receivedAcceleration = 0;
char receivedCommand;
/* r = Start command(Observe which side to turn)
 * x = Clockwise
 * y= Counterclockwise
 * a= Set Acceleration
 * s= Stop the motor
 */
bool newVal, runallowed = false; // runallowed flag for new value
AccelStepper stepperA(1,54, 55);
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Test");
stepperA.setMaxSpeed(500);
stepperA.setAcceleration(200);
stepperA.disableOutputs();

}

void continousRun2() {
  if (runallowed == true) {
    if (abs(stepperA.currentPosition()) < receivedStep)
    {
      stepperA.enableOutputs();
      stepperA.run();
    }
    else
    {
      runallowed = false ;
      stepperA.disableOutputs();
      Serial.print("Position: ");
      Serial.println(stepperA.currentPosition());
      stepperA.setCurrentPosition(0);
      Serial.print("Position: ");
      Serial.println(stepperA.currentPosition());
    }
  }
  else
  {
    return ;

  } 
}
void checkSerial() {
  if (Serial.available() > 0) {
    receivedCommand = Serial.read();
    newVal = true;
  }
  if (newVal == true) {
    if(receivedCommand == 'r') {
      runallowed = true ;
      receivedStep = Serial.parseFloat();
      receivedSpeed = Serial.parseFloat();
      Serial.print(receivedStep);
      Serial.println("Steps");
      Serial.print(receivedSpeed);
      Serial.println("steps/second");
      stepperA.setMaxSpeed(receivedSpeed);
      stepperA.move(receivedStep);
    }
  if (receivedCommand == 'x') {
    runallowed = true;
    receivedStep = Serial.parseFloat();
    receivedSpeed = Serial.parseFloat();
    Serial.print(receivedStep);
    Serial.println("Steps in CounterClockwise");
    Serial.print(receivedSpeed);
    Serial.println("steps/second");
    stepperA.setMaxSpeed(receivedSpeed);
    stepperA.move(receivedStep);
  }
  if (receivedCommand == 'y') {
    runallowed = true;
    receivedStep = Serial.parseFloat();
    receivedSpeed = Serial.parseFloat();
    Serial.print(receivedStep);
    Serial.println("Steps in Clockwise");
    Serial.print(receivedSpeed);
    Serial.println("steps/second");
    stepperA.setMaxSpeed(receivedSpeed);
    stepperA.move(-1*receivedStep);
  }
  if (receivedCommand == 's') {
    runallowed = false;
    stepperA.setCurrentPosition(0);
    Serial.println("Emergency Stop");
    stepperA.stop();
    stepperA.disableOutputs();
  }
  if (receivedCommand == 'a') {
    runallowed = false;
    receivedAcceleration = Serial.parseFloat();
    stepperA.setAcceleration(receivedAcceleration);
    Serial.println("Acceleration updated");
  }
  }
  newVal = false;
}
void loop() {
  // put your main code here, to run repeatedly:
  checkSerial() ;
  continousRun2();
}

Can anyone help me to handle this issue?

global1:
Can anyone help me to handle this issue?

The first of your AccelStepper programs has this line

stepperA.setSpeed(10000);

try changing it to

stepperA.setSpeed(10);

and see what happens

...R

I have changed it but same problem still exists

global1:
I have changed it but same problem still exists

Please post the revised program so we can be sure we are all looking at the same code.

…R

#include <AccelStepper.h>
AccelStepper stepperA(1,54, 55);
int steps = 10000;
int X = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Test");
 if (X < 2)
    {
        stepperA.move(steps);
        stepperA.setSpeed(10);
        stepperA.runSpeedToPosition();
        delay(2000);
        X = X + 1;
    }

}

void loop() {
  // put your main code here, to run repeatedly:

}

In your Original Post you included code that works - I presume it still works with that code?

If it does then I'm afraid I can't see any reason why the latest AccelStepper code is not working.

You could tidy it up be removing the IF clause which cannot have any effect in setup()

...R

The normal way to run AccelStepper is this:

use setAcceleration() and setMaxSpeed() in setup()

call run() from loop(), do not use any blocking code like delay, so that run() is called as frequently as
possible.

To initiate a move call move() or moveTo(). For an unscheduled stop call stop().

That's it, nice and simple, the library does the work for you and you don't have to worry about anything
except tuning the max speed and acceleration for optimal performance without miss-stepping.

MarkT:
The normal way to run AccelStepper is this:

Agreed. But is there any reason why the code the OP is using won't work? It seems to be very simple.

...R

I tried some changes in the working code. To understand issue deeply, I try to delete “ledpin output” which connected to enable pin on A4988 driver. Motor stops working after that. I do not know how to apply this output to Accelstepper because we just entry 2 pins as 55 and 54. So maybe because of that. Another idea is, when i look into Accelstepper library, i saw that there is a definition which called _cn and it arranges stepping in microseconds. It equals 1.000.000 maybe because of that equation. Also I use 1/16 microstepping by shortcutting MS1 MS2 MS3 pins

byte directionPin = 55;
byte stepPin = 54;
int numberOfSteps = 2000;
byte ledPin = 38; /* tried to delete "ledpin output" which connected to enable pin on A4988 driver. Motor stopped working) /*
int pulseWidthMicros = 700;  // microseconds /*Motor works abnormal out of these limits(500-1000)



void setup()
{

  Serial.begin(9600);

 
  delay(2000);

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

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

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

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(pulseWidthMicros);
   
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
 
}

void loop()
{

}
	// First step from stopped
	_cn = _c0;
	_direction = (distanceTo > 0) ? DIRECTION_CW : DIRECTION_CCW;
    }
    else
    {
	// Subsequent step. Works for accel (n is +_ve) and decel (n is -ve).
	_cn = _cn - ((2.0 * _cn) / ((4.0 * _n) + 1)); // Equation 13
	_cn = max(_cn, _cmin); 
    }
    _n++;
    _stepInterval = _cn;
    _speed = 1000000.0 / _cn;
    if (_direction == DIRECTION_CCW)
	_speed = -_speed;

AccelStepper doesn't concern itself with enable - that's your resonsibility.