# I want my stepper motor to switch speed while traveling (not acceleration wise)

I have this project I'm working on where I'll need the speed of the stepper motor to change set speed at a certain distance, I just can't figure out a way to do it. I'm using arduino and a stepper motor, this is the current code.

#include <AccelStepper.h>

AccelStepper stepper1(AccelStepper::FULL4WIRE, 0, 1, 2, 3);

void setup()
{
stepper1.setMaxSpeed(200.0);
stepper1.setAcceleration(400.0);
stepper1.moveTo(5000);

}

void loop()
{
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());
stepper1.run();


What I want it to do basically is to first moveTo(2500) at the current speed 200 then after 2500 I want it to increase speed to 400. After it has moved 5000 it turns and moves back to position but that's implemented already.

• Can you explain the exact wiring you have between the Arduino and the stepper motor? Are you using a stepper driver board (PCB), or are the coils for the stepper motor plugged directly into your Arduino? – Andrew Capodieci Oct 14 '13 at 23:23
• The arduino(UNO) is connected to a double H-bridge. From there it's connected to a 4 wire nema 23 stepper motor. The H-bridge has it's own power supply. – hollander Oct 15 '13 at 10:54
• Can't you just make another call to stepper1.setMaxSpeed(400) in an if statement: if(stepper1.currentPosition() == 2500) { stepper1.setMaxSpeed(400)) } – Andrew Capodieci Oct 15 '13 at 22:04
• Thank you very much! It works great. There's one thing with it that I didn't expect though. It first moves 2500 at one speed, then accelerates at 2500 speed, then the direction reverses and it goes back to start position, but after that it doesn't accelerate when it goes forward again. Is there anything I can do about that? – hollander Oct 17 '13 at 11:36

For this task; increase speed when a certain distance is reached.

I would use encoders on the wheels which will workout the distance travelled and when the target distance is reached then increase speed.

You can do what yu want with the standard arduino "Stepper" library. You are using something called "accelsteper.h".

Using the standard one....

This c-like psudocode will run a motor at some speed for 2000 steps then run and some other speed for 4000 steps and then go back an do it again.

loop{ setspeed(speed1) step(2000) setspeed(speed2) step(4000) }

Look here for details on the standard stepper library http://arduino.cc/en/Reference/Stepper

Get this to work FIRST. then make it nicer if you need to. for example place a half way between speed in there like this "set speed((speed1+speed2)/2). and run a few steps.

#include <AccelStepper.h>

//AccelStepper Xaxis(1, 2, 5); // pin 2 = step, pin 5 = direction
AccelStepper Xaxis(1, 12, 6); // pin 3 = step, pin 6 = direction

void setup()
{
Xaxis.setMaxSpeed(4000);

//Xaxis.setSpeed(10);
pinMode (3, INPUT);
}

void loop()
{
//Xaxis.runSpeed();
{
//Xaxis.stop();
Xaxis.setSpeed(20);

Xaxis.runSpeed();
}