// Include the AccelStepper Library
#include <AccelStepper.h>
// Define pin connections
const int dirPin = 2;
const int stepPin = 4;
// Define motor interface type
#define motorInterfaceType 1
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
Serial.begin(115200);
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(50);
myStepper.setSpeed(200);
myStepper.moveTo(200);
}
void loop() {
Serial.println("Current Step: " + String(myStepper.currentPosition()));
Serial.println("Remaining Step: " + String(myStepper.distanceToGo()));
// Change direction once the motor reaches target position
if (myStepper.distanceToGo() == 0) {
if (myStepper.currentPosition() == 200)
myStepper.moveTo(0);
else if (myStepper.currentPosition() == 0)
myStepper.moveTo(200);
}
// Move the motor one step
myStepper.run();
}