// ****************************** // Sketch by R. Jordan Kreindler, written October 2016 // to work with A4988 stepper driver module // Rotates stepper in full, 1/2, 1/4, 1/8, and 1/16 step modes // with the parameter "rotations" specifying how many times to // rotate for each stepping mode // ****************************** int oneLED = 4; // Full step LED int onehalfLED = 5; // 1/2 step LED int onequarterLED = 6; // 1/4 step LED int oneeighthLED = 7; // 1/8 step LED int onesixteenthLED = 8; // 1/16 step LED int ms1Pin = 9; // A4988 MS1 to Arduino digital pin 9 int ms2Pin = 10; // A4988 MS2 to Arduino digital pin 10 int ms3Pin = 11; // A4988 MS3 to Arduino digital pin 11 int stepPin = 12; // A4988 STEP pin to Arduino digital pin 12 int dirPin = 13; // A4988 DIR pin to Arduino digital pin 13 int numSteps = 48; // Number of steps in 360 degree rotation int rotations = 4; // Number of rotations of the rotor for each int delay1 = 15; // Delay between coil activations (ms) int delay2 = 2000; // Delay between subsequent rotations int i; // int to use in for loop //------------------------------ void setup() { pinMode(ms1Pin, OUTPUT); // MS1 set to receive Arduino signals pinMode(ms2Pin, OUTPUT); // MS2 set to receive Arduino signals pinMode(ms3Pin, OUTPUT); // MS3 set to receive Arduino signals pinMode(stepPin, OUTPUT); // stepPin set to receive Arduino signals pinMode(dirPin, OUTPUT); // DIR set to receive Arduino signals pinMode(oneLED, OUTPUT); // Full step LED pinMode(onehalfLED, OUTPUT); // 1/2 step LED pinMode(onequarterLED, OUTPUT); // 1/4 step LED pinMode(oneeighthLED, OUTPUT); // 1/8 step LED pinMode(onesixteenthLED, OUTPUT); // 1/16 step LED } //------------------------------ void loop() { delay(delay2); digitalWrite(dirPin, LOW); digitalWrite(oneLED, HIGH); // Rotate stepper rotatations revolutions, using full step method digitalWrite(ms1Pin, LOW); digitalWrite(ms2Pin, LOW); digitalWrite(ms3Pin, LOW); for (i = 1; i <= (numSteps * 1 * rotations); ++i) { digitalWrite(stepPin, LOW); // Prepare to take a step digitalWrite(stepPin, HIGH) ; // Take a step delay(delay1); // Allow some delay between energizing // the coils to allow stepper rotor time to respond. } delay(delay2); digitalWrite(oneLED, LOW); digitalWrite(dirPin, HIGH); digitalWrite(onehalfLED, HIGH); // Rotate stepper rotatations revolutions, using half step method digitalWrite(ms1Pin, HIGH); digitalWrite(ms2Pin, LOW); digitalWrite(ms3Pin, LOW); for (i = 1; i <= (numSteps * 2 * rotations); ++i) { digitalWrite(stepPin, LOW); // Prepare to take a step digitalWrite(stepPin, HIGH) ; // Take a step delay(delay1); // Allow some delay between energizing // the coils to allow stepper rotor time to respond. } digitalWrite(onehalfLED, LOW); delay(delay2); digitalWrite(dirPin, LOW); digitalWrite(onequarterLED, HIGH); // Rotate stepper a rotatations revolutions, using quarter stepping digitalWrite(ms1Pin, LOW); digitalWrite(ms2Pin, HIGH); digitalWrite(ms3Pin, LOW); for (i = 1; i <= (numSteps * 4 * rotations); ++i) { digitalWrite(stepPin, LOW); // Prepare to take a step digitalWrite(stepPin, HIGH) ; // Take a step delay(delay1); // Allow some delay between energizing // the coils to allow stepper rotor time to respond. } digitalWrite(onequarterLED, LOW); // Rotate stepper rotations revolution, using microstepping // 8 steps/step delay(delay2); digitalWrite(dirPin, HIGH); digitalWrite(oneeighthLED, HIGH); digitalWrite(ms1Pin, HIGH); digitalWrite(ms2Pin, HIGH); digitalWrite(ms3Pin, LOW); for (i = 1; i <= (numSteps * 8 * rotations); ++i) { digitalWrite(stepPin, LOW); // Prepare to take a step digitalWrite(stepPin, HIGH) ; // Take a step delay(delay1); // Allow some delay between energizing // the coils to allow stepper rotor time to respond. } digitalWrite(oneeighthLED, LOW); // Rotate stepper rotations revolution, using microstepping // 16 steps/step delay(delay2); digitalWrite(onesixteenthLED, HIGH); digitalWrite(dirPin, LOW); digitalWrite(ms1Pin, HIGH); digitalWrite(ms2Pin, HIGH); digitalWrite(ms3Pin, HIGH); for (i = 1; i <= numSteps * 16 * rotations; ++i) { digitalWrite(stepPin, LOW); // Prepare to take a step digitalWrite(stepPin, HIGH) ; // Take a step delay(delay1); // Allow some delay between energizing // the coils to allow stepper rotor time to respond. } digitalWrite(onesixteenthLED, LOW); }