It’s been a few months since I started learning to code for Arduino. I feel like I have a handle on the basics. Maybe I’d call myself an advanced beginner. I’ve done a lot of Paul McWhorter tutorials and have been able to use those skills to create some of my own projects that aren’t just following a tutorial and require unique problem solving.
However libraries in general are still confusing to me because they seem to all have their own new coding and rules. I’m trying to figure out how to use AccelStepper library so that I can implement a stepper motor without using delay. I’ve followed a tutorial that works as intended, but unlike learning the rest of Arduino the AccelStepper tutorials don’t seem to do as good of a job explaining how to use the library for anything other than the exact thing in the tutorial. What I really want is to be able understand how to use the library in other applications.
Specifically I want to turn a stepper motor back and forth. I want the range of rotation to be determined by reading data from a potentiometer and I want the speed to be determined by another potentiometer. The tutorials I’ve found so far tend to rely on user input from the serial monitor which is not what I need.
Can anyone point me toward some AccelStepper knowledge that explains all the functions? For example on the Arduino website for each function it’s both explained and they give a description, syntax, parameters, example code, and warnings. AccelStepper does not have as thorough documentation.
And generally do libraries become easier to pickup and understand with time? Or do they all have to be studied and picked apart to apply to use?
Do you know this documentation of Accelstepper?
Depends on the library. Some are well documented, some are not ... Some are easy to use and understand, and some less easy to understand
The more you know about C++ the easier it is to understand libraries. Some need to look into the code
How married are you to the AccelStepper library?
@MicroBahner has written the MobaTools library that has a stepper class. I suppose that he did not mention it because he is too humble. He need not be as I think that his library is easier to learn and use than AccelStepper for many applications. He has good documentation and there are several examples (some in German, but Google translate helps with that).
One pot to control distance, one pot to control speed. Stepper bounces back and forth using MobaTools library.
#include <MobaTools.h>
const byte distancePin = A0;
const byte speedPin = A1;
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8; // for cnc shield
const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;
MoToStepper stepper( STEPS_REVOLUTION, STEPDIR );
void setup()
{
Serial.begin(115200);
stepper.attach( stepPin, dirPin );
stepper.attachEnable(enablePin, 0, 1);
stepper.setSpeed(1000); // rpm /10
stepper.setRampLen(50);
stepper.setZero();
}
void loop()
{
static int sign = 1;
int speed = analogRead(speedPin);
int distance = analogRead(distancePin);
stepper.setSpeed(speed);
if(stepper.moving() == 0)
{
sign = sign * -1; // reverse motor
stepper.move(distance * sign);
}
}
Same program using AccelStepper:
#include <AccelStepper.h>
const byte distancePin = A0;
const byte speedPin = A1;
const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8; // for CNC shield
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
void setup()
{
pinMode(enablePin, OUTPUT); // for CNC shield
digitalWrite(enablePin, LOW); // enable steppers
// Change these to suit your stepper if you want
stepper.setMaxSpeed(500);
stepper.setAcceleration(200);
}
void loop()
{
static int sign = 1;
int speed = analogRead(speedPin);
int distance = analogRead(distancePin);
stepper.setMaxSpeed(speed);
// If at the end of travel go to the other end
if (stepper.run() == LOW) // if the stepper has stopped
{
sign = sign * -1; // reverse sign
stepper.move(distance * sign);
}
}
Scale the speed and distance to your liking with the map() function.
Both programs successfully tested with my Uno, CNC shield, NEMA 17 stepper, DRV8825 stepper driver and 2 10K pots.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.