// Include the AccelStepper Library
#include <AccelStepper.h>
// Define pin connections
const int dirPin = 0;
const int stepPin = 1;
// Define motor interface type
#define motorInterfaceType 1
#define INTERRUPTPIN 2
// Creates an instance
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);
void blink() {
myStepper.moveTo(100);
}
void setup() {
pinMode(INTERRUPTPIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(INTERRUPTPIN),blink,FALLING);
// set the maximum speed, acceleration factor,
// initial speed and the target position
myStepper.setMaxSpeed(1000);
myStepper.setAcceleration(5);
myStepper.setSpeed(200);
myStepper.moveTo(100);
}
void loop() {
// Change direction once the motor reaches target position
if (myStepper.distanceToGo() == 0)
myStepper.moveTo(0);
//myStepper.moveTo(-myStepper.currentPosition());
// Move the motor one step
myStepper.run();
}