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.
I am using AccelStepper Library
Here is my code:
#include <AccelStepper.h>
//Define stepper motor connections
#define dirPin 4
#define stepPin 14
//Create stepper object
AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.
void setup()
{
stepper.setMaxSpeed(1200); //maximum steps per second
}
void loop()
{
stepper.setSpeed(1200); //steps per second
stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
}
I have tried to add delay and counterclockwise code but its not working.
#include <AccelStepper.h>
//Define stepper motor connections
#define dirPin 4
#define stepPin 14
//Create stepper object
AccelStepper stepper(1,stepPin,dirPin); //motor interface type must be set to 1 when using a driver.
void setup()
{
stepper.setMaxSpeed(1200); //maximum steps per second
}
void loop()
{
Serial.println("clockwise");
stepper.setSpeed(1200); //steps per second
stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
delay(1000);
Serial.println("counterclockwise");
stepper.setSpeed(-1200); //steps per second
stepper.runSpeed(); //step the motor with constant speed as set by setSpeed()
delay(1000);
}
2 Answers 2
Hi here it the reference to the library functions that are available in accelstepper library.
Go through the link, it will provide you the issues with your code and example for your application like @smajli said steps more than 1000 is unreliabe, here you can see setSpeed(float)
function where setting negative or positive sign selects the direction and value set the speed of rotation.
-
than you for your input but i am still struggling to understand functions :( i have changed speed to 1000 and behavior is still sameHardy– Hardy2019年05月30日 15:15:21 +00:00Commented May 30, 2019 at 15:15
-
did you tried -500 and 500 ?Vaibhav– Vaibhav2019年05月31日 16:13:01 +00:00Commented May 31, 2019 at 16:13
-
yes buddy seems i can't use delay function in loop but i am not getting how to use other function of this library or use millis functionHardy– Hardy2019年05月31日 18:55:26 +00:00Commented May 31, 2019 at 18:55
From the AccelStepper website: link
Speeds of more than 1000 steps per second are unreliable
Try to change the speed to lower value, and let us know what is the outcome.
-
thank you for pointing out but still same behaviorHardy– Hardy2019年05月30日 15:14:14 +00:00Commented May 30, 2019 at 15:14
it rotate only in one direction
... which direction is that?