Accelerating to a constant speed

843 views
Skip to first unread message

Taha Bintahir

unread,
Sep 18, 2014, 4:33:26 PM9/18/14
to accels...@googlegroups.com
Hi so what i'm trying to do is when you press a button accelerate the stepper to a constant speed and let it run and when the button is pressed again it slows back down and stops. So below i have some code to accelerate the motor, but what I find is that after the x number of steps i've specified in moveTo() it stops. how can i make it continually run a a constant speed.

#include <AccelStepper.h>

int motorSpeed = 10000; //maximum steps per second (about 3rps / at 16 microsteps)
float motorAccel = 100; //steps/second/second to accelerate

int motorDirPin = 2; //digital pin 2
int motorStepPin = 3; //digital pin 3

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 


void setup()
{
  Serial.begin(9600);
}

void loop()
   stepper.setCurrentPosition(0);
   stepper.setMaxSpeed(motorSpeed);
  
   stepper.setAcceleration(motorAccel);
  
   stepper.moveTo(100000);

   while (stepper.run()){
     Serial.println(stepper.currentPosition());}
   
}

Another thing that i found was that if I run the motor at a constant speed using the code below it the motor runs faster than the code above. Any specific reason for that? Both pieces of code set the  setMaxSpeed().

#include <AccelStepper.h>

int motorSpeed = 10000; //maximum steps per second (about 3rps / at 16 microsteps)

int motorDirPin = 2; //digital pin 2
int motorStepPin = 3; //digital pin 3

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin); 


void setup()
{
   stepper.setMaxSpeed(motorSpeed);
   stepper.setSpeed(motorSpeed);
}

void loop()
{  
   stepper.runSpeed();
}

Thanks for any help you guys can provide.
 

Mike McCauley

unread,
Sep 18, 2014, 9:23:59 PM9/18/14
to accels...@googlegroups.com
Hello,

On Thursday, September 18, 2014 01:33:25 PM Taha Bintahir wrote:
> Hi so what i'm trying to do is when you press a button accelerate the
> stepper to a constant speed and let it run and when the button is pressed
> again it slows back down and stops. So below i have some code to accelerate
> the motor, but what I find is that after the x number of steps i've
> specified in moveTo() it stops. how can i make it continually run a a
> constant speed.

Try setting a very large target position in the direction you want to move.
Call stop() when you want to decelerate and stop.


>
> #include <AccelStepper.h>
>
> int motorSpeed = 10000; //maximum steps per second (about 3rps / at 16
> microsteps)
> float motorAccel = 100; //steps/second/second to accelerate
>
> int motorDirPin = 2; //digital pin 2
> int motorStepPin = 3; //digital pin 3
>
> //set up the accelStepper intance
> //the "1" tells it we are using a driver
> AccelStepper stepper(1, motorStepPin, motorDirPin);
>
>
> void setup()
> {
> Serial.begin(9600);
> }
>
> void loop()
> {
> stepper.setCurrentPosition(0);
> stepper.setMaxSpeed(motorSpeed);
>
> stepper.setAcceleration(motorAccel);
>
> stepper.moveTo(100000);
>
> while (stepper.run()){
> Serial.println(stepper.currentPosition());}
>
> }
>
> Another thing that i found was that if I run the motor at a constant speed
> using the code below it the motor runs faster than the code above. Any
> specific reason for that? Both pieces of code set the setMaxSpeed().

Probably the cost of the println


>
> #include <AccelStepper.h>
>
> int motorSpeed = 10000; //maximum steps per second (about 3rps / at 16
> microsteps)
>
> int motorDirPin = 2; //digital pin 2
> int motorStepPin = 3; //digital pin 3
>
> //set up the accelStepper intance
> //the "1" tells it we are using a driver
> AccelStepper stepper(1, motorStepPin, motorDirPin);
>
>
> void setup()
> {
> stepper.setMaxSpeed(motorSpeed);
> stepper.setSpeed(motorSpeed);
> }
>
> void loop()
> {
> stepper.runSpeed();
> }
>
> Thanks for any help you guys can provide.

--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

Reply all
Reply to author
Forward
0 new messages