0

I'm relatively new to arduino thus need your help to figure out why my stepper motor doesn't rotate clockwise and counterclockwise. All I could do was to make it rotate only in one direction.

Here is my code:

#include <AccelStepper.h>

int pos=16000;
AccelStepper stepper(1,4,5);

void setup() {
stepper.moveTo(pos);
stepper.setMaxSpeed(6400);
stepper.setSpeed(6400);
}

void loop() {
if (stepper.distanceToGo() == 0)
{   
  stepper.setCurrentPosition(0);  
  pos = -pos;  
  stepper.moveTo(pos);  
  stepper.setSpeed(6400);
}
   stepper.runSpeed();
}

Will appreciate any help you can give me :)

1

The “Performance” paragraph of the “Detailed Description” section of AccelStepper documentation says:

The fastest motor speed that can be reliably supported is about 4000 steps per second at a clock frequency of 16 MHz on Arduino such as Uno etc. ...

and the AccelStepper::setSpeed () documentation says:

... Speeds of more than 1000 steps per second are unreliable

Try changing your 6400-steps-per-second setting to a some plausible number, eg 500 or 1000.

Actually, it would make sense to start out with moderate destination settings and speeds, like a target position of 200 instead of 16000, and steps-per-second of 100, or whatever count it takes for say exactly two shaft revolutions. Once you have that working right, then start doubling numbers until it no longer works, after which you can back off by a factor of say four (to allow for variations in load, etc).

  • Thxs, I'll try to lower a speed – Iryna D Apr 14 '16 at 0:59
1

Actually after trying different options, I could finally find a solution that seems to work for me:

if (stepper.distanceToGo() == 0 && stepper.currentPosition()==50000) {
    delay(1000);
    stepper.setCurrentPosition(0);
    stepper.moveTo(-50000);
    stepper.setSpeed(10000);
    stepper.runSpeedToPosition();
  }
  else if (stepper.distanceToGo() == 0 && stepper.currentPosition()==-50000) {
    delay(1000);
    stepper.setCurrentPosition(0);
    stepper.moveTo(50000);
    stepper.setSpeed(10000);
    stepper.runSpeedToPosition();
  }
  stepper.runSpeed();
  • Just out of interest, you are still using a speed which is an order of magnitude greater than 1000, which as jwpat points out is unreliable. What happened when you used speeds of <1000? Did it not work? – Greenonline Apr 15 '16 at 9:44
  • 1
    Yes, I'm still using high speeds, but I guess it works for me because my stepper motor is configured on a micro step. If I put a speed lower than 1000 it also works, but very slowly – Iryna D Apr 18 '16 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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