Pages: [1]   Go Down
Author Topic: Problem with using accelstepper library  (Read 133 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
I am working on a project where I have to make a motor move for a certain number of steps then stop. I know how to make it move but I dont know how to make it stop. it just keeps going on and on, can someone help me make it stop?

thanks
Code:
#include <AccelStepper.h>

AccelStepper motor(1,2,3); // step pin = 2 dir pin = 3
double velocity=900;
double distance=5000;
double acceleration=1000;
int mode=2;

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

void loop()
{
  if (mode==1)
  {
     motor.setCurrentPosition(0); 
     moveup();
  }
  if (mode==2)
  {
     motor.setCurrentPosition(0); 
     movedown();
  }
}

void moveup()
{
  while(mode==1)
  {
    motor.setMaxSpeed(velocity);
    motor.setAcceleration(acceleration);
    motor.move(-distance);
    motor.run();
  }   
}


void movedown() 
{
  while(mode==2)
  {
    motor.setMaxSpeed(velocity);
    motor.setAcceleration(acceleration);
    motor.move(distance);
    motor.run();
  }
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8937
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you need a mode==3 where it doesn't move.

Your code starts in mode==2 and thus moves downward repeatedly.  It never changes to any other mode.  If you only want to move 5000 steps once you should probably switch to mode==3 as soon as you initiate the downward move.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

UK
Offline Offline
Tesla Member
***
Karma: 134
Posts: 7612
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't seem to have any code to change the mode.

My guess is that you need to create a checkMode() function and call it from loop(). Then you will find that it doesn't get called while a motor is moving due to the WHILE loop in the moveup() and movedown() functions. That can be solved by moving the call to motor.run() into loop() and dispensing with the WHILEs in the functions.

...R
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know how to make it move but I dont know how to make it stop. it just keeps going on and on, can someone help me make it stop?

Instead of telling the stepper to move by a given offset by move() , tell it to move to a specified position by moveTo(). Then it will stop itself.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50111
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then it will stop itself.
Of course, the infinite loop won't end. But, at least the stepper will hold still.

Dealing with the infinite loops would be the first step. Then, deal with using the proper methods.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all so much, I changed it to moveTo() and it worked.
Thanks again.
Logged

Pages: [1]   Go Up
Jump to: