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 :)

asked Apr 11, 2016 at 23:37

2 Answers 2

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).

answered Apr 12, 2016 at 6:39
1
  • Thxs, I'll try to lower a speed Commented Apr 14, 2016 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();
answered Apr 14, 2016 at 1:08
2
  • 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? Commented Apr 15, 2016 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 Commented Apr 18, 2016 at 22:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.