Stepper motor programming | AccelStepper

Hi,

First of all, for me programming of the Arduino board and controlling a stepper motor is totally new.

The stepper motor I'm programming has to move a certain time with a certain speed forward, stop, than move a certain time with a certain speed backward. When this "simple" movement works correctly I'd like to tune the time and speed parameters with a pot meter. I tried several things but I do not succeed to get the correct void loop script. Underneath one of the scripts I used. This is the script with constant time and speed parameters. I'm making use of AccelStepper add in.

Probably one of you can give me a small push into the correct direction.

Waiting on your response.

#include <AccelStepper.h>

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper (1,3,4); // name of stepper motor (1 = driver, pin 3 = step, pin 4 = direction) 


void setup() {
    stepper.setMaxSpeed(400000);
    stepper.setAcceleration(400000);
}

void loop()
{
stepper.stop();
stepper.moveTo(stepper.currentPosition()+100);
stepper.run();
stepper.moveTo(stepper.currentPosition()-500);
stepper.run();
}

The next step is to add some code to read the pin(s) that the potentiometer(s) are connected to.

Then, you need to decide how to use the value(s) to influence the speed and time, keeping in mind that you are NOT determining independently how long the stepper steps. You are simply defining the number of steps to take. With a given maximum acceleration and speed, that amounts to defining how long it takes. But, when you change the speed, you'll change the time, in a way that is not independent, unless one pot controls speed and the other controls the number of steps.

PaulS, thanks for your reply.

First of all. I want to start with a easy script, so no use is made of pot values. I will implement this in a later stage.

When I upload the scrip given above the motor does not go 100 steps forward and 500 backward. The motor turns in one direction. What goes wrong?

I understand that distance speed and acceleration are time derivatives of each other and so dependent and will use a correct amount of pot meters to tackle this.

When I upload the scrip given above the motor does not go 100 steps forward and 500 backward. The motor turns in one direction. What goes wrong?

You tell the stepper to go to a position (moveTo()). Then, you tell it to take one step (run()). Then, you define somewhere else for the motor to be (moveTo()) and tell it to take another step (run()). Then, the process repeats.

Of course, the stepper is going crazy trying to get somewhere.

Do not define a new position for the stepper until it has gotten to the last place you told it to go to.

Thanks for the fast response.

In stead of "run()" command I now use "runToPosition()". I did not know that the first command only result in 1 step movement.

I'm surprised it moves at all with the huge velocity and accel numbers you gave it. I don't know any stepper motor that can come close to 400K steps/second. Most steppers are good for, at best, with no load, between 1000-2000 RPM, or about 6-7000 steps/second. Nor is the Arduino likely to be capable of issuing pulses even that fast using AccelStepper. In reality, you'll be lucky to get to even 1000RPM with AccelStepper.

I suggest you start by tuning it WAY down (like a few hundred RPM at best, with a slow accel), until you at least get it working properly. Then start seeing what it can actually handle for velocity and accel, after you're sure the code is right.

It will also be very important to call run() at a near constant rate, which means not doing much else when the motor is actually moving. Otherwise, you'll find the motor suddenly stops dead and just growls in the middle of moves, due to jitter in the step pulse timing causing stalls.

Regards,
Ray L>

Have a look at stepper motor basics and this simple stepper code.

...R