Stepper Test
This commit is contained in:
parent
f2c95ed630
commit
403a646c14
1 changed files with 36 additions and 0 deletions
36
StepperTest/StepperTest.ino
Normal file
36
StepperTest/StepperTest.ino
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*Example sketch to control a stepper motor with DRV8825 stepper motor driver, AccelStepper library and Arduino: number of steps or revolutions. More info: https://www.makerguides.com */
|
||||
|
||||
// Include the AccelStepper library:
|
||||
#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
|
||||
#define microStepsFactor 4
|
||||
|
||||
// Create a new instance of the AccelStepper class:
|
||||
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
|
||||
|
||||
int dir = 1;
|
||||
|
||||
void setup() {
|
||||
// Set the maximum speed in steps per second:
|
||||
stepper.setMaxSpeed(1000*microStepsFactor);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Set the current position to 0:
|
||||
stepper.setCurrentPosition(0);
|
||||
|
||||
int revs = random(1,5);
|
||||
dir = -dir;
|
||||
// Run the motor forward at 200 steps/second until the motor reaches 400 steps (2 revolutions):
|
||||
while(stepper.currentPosition() != 200 * microStepsFactor * revs * dir) {
|
||||
stepper.setSpeed(1000 * microStepsFactor * dir);
|
||||
stepper.runSpeed();
|
||||
}
|
||||
|
||||
delay(200);
|
||||
}
|
Loading…
Reference in a new issue