3
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$ Oct 14, 2013 at 23:23
  • $\begingroup$ 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. $\endgroup$
    – hollander
    Oct 15, 2013 at 10:54
  • $\begingroup$ Can't you just make another call to stepper1.setMaxSpeed(400) in an if statement: if(stepper1.currentPosition() == 2500) { stepper1.setMaxSpeed(400)) } $\endgroup$ Oct 15, 2013 at 22:04
  • $\begingroup$ 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? $\endgroup$
    – hollander
    Oct 17, 2013 at 11:36

3 Answers 3

3
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$
2
$\begingroup$
#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();
   if (digitalRead (3) == HIGH)
   {
     //Xaxis.stop();
     Xaxis.setSpeed(20);

     Xaxis.runSpeed();
   }

   if (digitalRead (3) == LOW)
   {
     Xaxis.runSpeed();
     Xaxis.setSpeed(-10);  // - sign change direction !!!!!
   }
}
$\endgroup$
1
  • $\begingroup$ Welcome Gerd - a little bit of explanation would be useful :-) $\endgroup$
    – Andrew
    Apr 8, 2015 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.