AccelStepper - HELP

Looks like AccelStepper is some kind of meme, coz nobody actually knows how to use it, or anyone who finds out dies instantly.

I wasted hours on searching for working sketch for bipolar stepper, but all I find is more and more topics without a solution.

I'm using Arduino Mega (Marlin firmware) + RAMPS 1.4 + DRV8825 + Nima17 stepper.

In example from accelstepper site there is no clear description on which 4 pins should be used and in what order.
There are basically 3 pins to be involved: step, direction and enable. In the example 4 is a must.

I checked test code from reprap ramps wiki and it works, I.e. stepper moves slowly (not quite smooth though).

Could anyone help make this magical accelstepper work or its really just a meme to make newcomers ragequit a this arduino motor stuff?

Ramps wiki RAMPS 1.4 - RepRap
AccelMeme site AccelStepper: AccelStepper library for Arduino

Call setMaxSpeed() and setAcceleration() in setup(),
call run() in loop()
don't use delay.

You can then issue commands (like moveTo()) whenever you like and it all happens in the background.

The crucial thing is that run() is called very frequently (hence no delays). The AccelStepper library drives
everything automatically for you in the run() call.

USERNAME_TOO_SHORT:
In example from accelstepper site there is no clear description on which 4 pins should be used and in what order.

With a DRV8825 you only need two pins (step and direction) plus enable. You should use the DRIVER option in AccelStepper.

...R

Thank you for your answers, all you mentioned is correct and logical but unfortunately I still couldn't make my code run the motor.

Could it be the enable pin that is not involved into the code?

My code is:

#include <AccelStepper.h>

AccelStepper Motor(AccelStepper::DRIVER, 46, 48);

void setup() {
Motor.setAcceleration(200);
Motor.setMaxSpeed(500);
}

void loop() {
Motor.run();
}

I also tried runSpeed

I also tried:

void setup() {
Motor.setAcceleration(5000);
Motor.setMaxSpeed(400);
Motor.moveTo(1000);
Motor.setEnablePin(62); //62 is enable pin for my setup
Motor.enableOutputs();
}

And same loop. Still no move

Can you take the DRV8825 out of the RAMPS board and plug it into a breadboard so that you can see exactly what Arduino pins connect to the DRV8825 pins.

According to the Pololu DRV8825 web page the enable and sleep pins should both be connected to 5v (or an I/O pin set as OUTPUT and HIGH).

Then you can try to get the motor working with this Simple Stepper Code which does not use any library.

You should be able to get the motor working with the driver in the RAMPS board with the same program as long as you change the code to use the appropriate pins.

If you can confirm that the motor works with my simple stepper code it will make it easier to figure out the AccelStepper problem.

...R

Robin2:
Can you take the DRV8825 out of the RAMPS board and plug it into a breadboard so that you can see exactly what Arduino pins connect to the DRV8825 pins.

According to the Pololu DRV8825 web page the enable and sleep pins should both be connected to 5v (or an I/O pin set as OUTPUT and HIGH).

Then you can try to get the motor working with this Simple Stepper Code which does not use any library.

You should be able to get the motor working with the driver in the RAMPS board with the same program as long as you change the code to use the appropriate pins.

If you can confirm that the motor works with my simple stepper code it will make it easier to figure out the AccelStepper problem.

...R

The motor works with the TEST CODE example from
https://reprap.org/wiki/RAMPS_1.4
So in terms of pins and hw connections I can confirm everything looks fine. The issue with accelstepper is there is no clear example of calling enable pin

I can't think of any reason why you should not assert the enable pin with your own code separately from the AccelStepper library.

I wonder if you have got yourself into a knot simply because you are over-thinking the problem and the capabilities of AccelStepper. :slight_smile:

...R

Robin2:
I can't think of any reason why you should not assert the enable pin with your own code separately from the AccelStepper library.

I wonder if you have got yourself into a knot simply because you are over-thinking the problem and the capabilities of AccelStepper. :slight_smile:

...R

I'm engineer, I can't sleep well until I make everything work right

Now I need to implement stepper control over serial port.
What I need:
If value ( "1", or "+1", or "111" - value with several digits is prefered for further modifications) is received via serial, motor will move for the number of steps equal to value (+/- before value gives direction)

So far I cam across some code that makes stepper move to poarticular number of steps. I modified it to apply 2 scenarios: move 500 steps forward and run at speed 1000 continiously.

The code seems to be wrong, when I send either "a" or "b" to serial (via serial monitor), stepper moves 1 step instea of what ever value I set in the code.
Here is the code I wrote based on examples from internet:

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::DRIVER, 54, 55);

int val;
int stepper1_pos = 0; //initial position of stepper motor
int stepper1_steps = 1000; //steps per input

void setup()
{
Serial.begin(9600);
stepper1.setMaxSpeed(1000);
stepper1.setEnablePin(38);
stepper1.setPinsInverted(false, false, true);
}

void loop()
{
if (Serial.available()) // if serial value is available
{
val = Serial.read(); // read input from serial
if (val == 'a')
{
Serial.println("test point 1");
stepper1.setSpeed(100);
stepper1_pos += stepper1_steps; //set motor position to stepper1_steps
stepper1.moveTo(stepper1_pos); //move to stepper1_pos position
stepper1.enableOutputs();
stepper1.run();
}
else if (val == 'b')
{
Serial.println("test point 2");
stepper1.setSpeed(1000);
stepper1.enableOutputs();
stepper1.runSpeed();
}
}
}

I saw in some topics that using enableOutputs(), runSpeed() and run() inside IF is not recommended (no idea why tho), but I'm not sure if the code is supposed to run corectly if I just put the all 1 by 1 :slight_smile:

Don't mix the code to receive data with the code to operate the stepper motor. Put them in separate functions and then you can test them separately.

Serial Input Basics - simple reliable ways to receive data.
Planning and Implementing a Program

...R

Robin2:
Don't mix the code to receive data with the code to operate the stepper motor. Put them in separate functions and then you can test them separately.

Serial Input Basics - simple reliable ways to receive data.
Planning and Implementing a Program

...R

Thanks, I'll take a look)

After reading this I'm absolutely confused and lost and nothing works at all :frowning:

Post the latest version of your program and describe in as much detail as possible what happens when you run it.

...R

Robin2:
Post the latest version of your program and describe in as much detail as possible what happens when you run it.

...R

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::DRIVER, 54, 55);

int pos = 0; // Defines relative position
int mov = 200; // Defines 200 steps the stepper will make clockwise or conter-clockwise if serial inpu is "c" or "d".

void setup()
{
Serial.begin(9600);
stepper1.setMaxSpeed(20000); //Sets the max speed for stepper 1
stepper1.setEnablePin(38); //Defines Enable pin
stepper1.setPinsInverted(false, false, true);
stepper1.setCurrentPosition(0); // Sets stepper's position to 0 once after power up
}

void loop()
{
if (Serial.available()) // Checks Serial input
{
if (stepper1.distanceToGo() == 0) // Checks if stepper has reached it's latest destination
{
if (Serial.read() == "a") // Checks if seral input is equal to "a"
{
stepper1.moveTo(0); // Sets move destination to 0 position
stepper1.setSpeed(10000);
//Serial.print("a");
}
else if (Serial.read() == "b") // Checks if seral input is equal to "b"
{
stepper1.moveTo(100); // Sets move destination to 100 (+100 steps rom 0 positon)
stepper1.setSpeed(10000);
//Serial.print("b");
}
if (Serial.read() == "c") // Checks if seral input is equal to "c"
{
pos += mov; // Sets destination for move() to +200 steps (mov = 200)
stepper1.move(pos); // Sets destination to +200 steps from current position
//Serial.print("c");
}
else if (Serial.read() == "d") // Checks if seral input is equal to "d"
{
pos -= mov; // Sets destination for move() to -200 steps (mov = 200)
stepper1.move(pos); // Sets destination to -200 steps from current position
//Serial.print("d");
}
}
}
else
{
stepper1.enableOutputs(); // Does something I don't quite understand...
stepper1.run(); // Makes the stepper make 1 step according to latest "if"
}
}

The idea is quite simple:
"a" moves stepper to absolute postion 0, which is set by setCurrentPosition(0) once at start
"b" moves stepper o absolute position 100
"c" moves sepper +200 steps from current position
"d" moves steper -200 steps from current position

This code atualy doesn't work, motor just makes annoyng noise and doesn't move at all.

I get the impression you have not tried any of my suggestions - for example to separate the Serial input from the stepper code or to use your own code to deal with the enable pin.

When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

...R

Robin2:
I get the impression you have not tried any of my suggestions - for example to separate the Serial input from the stepper code or to use your own code to deal with the enable pin.

When posting code please use the code button </>
codeButton.png

so your code 

looks like this



and is easy to copy to a text editor See [How to use the Forum](http://forum.arduino.cc/index.php?topic=149014.0)

...R

Believe me or not, separation did not help, it made things even less logical and understandable. I read all your links and still no light in the tunnel.
Tell me, if this code might work without separation?
If yes, please tell me, what is wrong here?
If no, then why not?

USERNAME_TOO_SHORT:
Believe me or not, separation did not help, it made things even less logical and understandable.

When I wrote Reply #14 I has expecting you to post the version of your program that gave rise to your Reply #13

I will not be offended if you choose not to follow my advice. All I can say in my defence is that my own programs work.

...R

Robin2:
When I wrote Reply #14 I has expecting you to post the version of your program that gave rise to your Reply #13

I will not be offended if you choose not to follow my advice. All I can say in my defence is that my own programs work.

...R

I just posted the whole code to give you a full picture of how I want it to work and expected you to point out any obvious mistakes. I'm 99% sure that not using separae functions for each IF is not the root cause of this code not working. I read accelstpper operators description 10+ times already but it still doesn't give me a feel of how ll this should actually work, so all I do is searching forums for new examples and answers, try them and SURPRISINGLY it still doesnt work. In best cases it doesn't work in some dfferent way.

I need to code just 4 operations I listet above and I couldn't imagine it would be so hard with aduino to make just 1 stepper go to X steps at Y speed and stop.