#include <AccelStepper.h>
// Define step constant
#define MotorInterfaceType 4
// Creates an instance
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper myStepper(MotorInterfaceType, 8, 10, 9, 11);
void setup() {
// Initialize serial communication
Serial.begin(9600);
// set the maximum speed, acceleration factor, initial speed and the target position
myStepper.setMaxSpeed(1000.0);
myStepper.setAcceleration(50.0);
myStepper.setSpeed(200);
myStepper.moveTo(2038);
// Wait for serial connection
while (!Serial) { ; }// Do nothing
// Print initialization message
Serial.println("Stepper motor initialized.");
}
void loop() {
// Change direction once the motor reaches target position
if (myStepper.distanceToGo() == 0) {
myStepper.moveTo(-myStepper.currentPosition()); }
// Move the motor one step
myStepper.run();
// Print current position to serial monitor
Serial.print("Current position: ");
Serial.println(myStepper.currentPosition());
// Delay for 10 milli second before moving again
delay(10);
}