Skip to content

Instantly share code, notes, and snippets.

@GreenMoonArt
Last active May 7, 2022 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GreenMoonArt/2a5e6a1ccfe19fe00a54a7f56ef2de73 to your computer and use it in GitHub Desktop.
Save GreenMoonArt/2a5e6a1ccfe19fe00a54a7f56ef2de73 to your computer and use it in GitHub Desktop.
Stepper Motor Back and Forth
// tutorial
// https://www.makerguides.com/a4988-stepper-motor-driver-arduino-tutorial/
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(50);
}
void loop() {
// Set the current position to 0:
stepper.setCurrentPosition(0);
while(stepper.currentPosition() != 30)
{
stepper.setSpeed(25);
stepper.runSpeed();
}
delay(200);
// Reset the position to 0:
stepper.setCurrentPosition(0);
// go the other direction
while(stepper.currentPosition() != -30)
{
stepper.setSpeed(-25);
stepper.runSpeed();
}
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment