Last active
May 7, 2022 13:55
-
-
Save GreenMoonArt/2a5e6a1ccfe19fe00a54a7f56ef2de73 to your computer and use it in GitHub Desktop.
Stepper Motor Back and Forth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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