// Example sketch to control a stepper motor with A4988 stepper motor driver
// and Arduino without a library.
// More info: https://www.makerguides.com
// Define use the Potentiometer attached to Analogue Pin 0 to set the speed:
#define SpeedPin A0
int WaitTime = 2000; //Set a default wait time of 2000ms / 2 seconds
int WaitMultiplier = 5; // Define a waitime multiplier, depending on the properties of the potentiometer
//Set Step and Direction Pins for Motor 1
#define M1dirPin 11
#define M1stepPin 12
//Set Step and Direction Pins for Motor 2
#define M2dirPin 8
#define M2stepPin 9
//Set Step and Direction Pins for Motor 3
#define M3dirPin 5
#define M3stepPin 6
#define StepsPerRevolution 200 //Assumes that all motors are the same and have 200 steps per revolution / 1.8 degree steps
void setup() {
// Declare motor pins as output:
pinMode(M1stepPin, OUTPUT);
pinMode(M1dirPin, OUTPUT);
pinMode(M2stepPin, OUTPUT);
pinMode(M2dirPin, OUTPUT);
pinMode(M3stepPin, OUTPUT);
pinMode(M3dirPin, OUTPUT);
// Declare potentiometer pin as input
pinMode(SpeedPin, INPUT);
}
void loop() { //The code will repeat indefinitely
//Present the first target by spinning the first motor clockwise
// Set the spinning direction for motor 1 clockwise:
digitalWrite(M1dirPin, HIGH);
// Spin the stepper motor 90 degrees:
for (int i = 0; i < StepsPerRevolution/4; i++) {
// These four lines result in 1 step as the stepper motor steps on the rising edge of a digital wave - Transition from Low to High:
digitalWrite(M1stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M1stepPin, LOW);
delayMicroseconds(2000);
}
//Wait before retracting the target, speed is set by the potentiometer
WaitTime=analogRead(SpeedPin) * WaitMultiplier;
delay(WaitTime);
//Retract the first target by spinning the first motor anti-clockwise
// Set the spinning direction for motor 1 anti-clockwise:
digitalWrite(M1dirPin, LOW);
// Spin the stepper motor 90 degrees:
for (int i = 0; i < StepsPerRevolution/4; i++) {
// These four lines result in 1 step as the stepper motor steps on the rising edge of a digital wave - Transition from Low to High:
digitalWrite(M1stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M1stepPin, LOW);
delayMicroseconds(2000);
}
//Wait before presenting the second target, speed is set by the potentiometer
WaitTime=analogRead(SpeedPin) * WaitMultiplier;
delay(WaitTime);
//Present the second target by spinning the second motor clockwise
// Set the spinning direction for motor 2 clockwise:
digitalWrite(M2dirPin, HIGH);
// Spin the stepper motor 90 degrees:
for (int i = 0; i < StepsPerRevolution/4; i++) {
// These four lines result in 1 step as the stepper motor steps on the rising edge of a digital wave - Transition from Low to High:
digitalWrite(M2stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M2stepPin, LOW);
delayMicroseconds(2000);
}
//Wait before retracting the second target, speed is set by the potentiometer
WaitTime=analogRead(SpeedPin) * WaitMultiplier;
delay(WaitTime);
//Retract the second target by spinning the second motor anti-clockwise
// Set the spinning direction for motor 2 anti-clockwise:
digitalWrite(M2dirPin, LOW);
// Spin the stepper motor 90 degrees:
for (int i = 0; i < StepsPerRevolution/4; i++) {
// These four lines result in 1 step as the stepper motor steps on the rising edge of a digital wave - Transition from Low to High:
digitalWrite(M2stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M2stepPin, LOW);
delayMicroseconds(2000);
}
//Wait before presenting the third target, speed is set by the potentiometer
WaitTime=analogRead(SpeedPin) * WaitMultiplier;
delay(WaitTime);
//Present the third target by spinning the third motor clockwise
// Set the spinning direction for motor 3 clockwise:
digitalWrite(M3dirPin, HIGH);
// Spin the stepper motor 90 degrees:
for (int i = 0; i < StepsPerRevolution/4; i++) {
// These four lines result in 1 step as the stepper motor steps on the rising edge of a digital wave - Transition from Low to High:
digitalWrite(M3stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M3stepPin, LOW);
delayMicroseconds(2000);
}
//Wait before retracting the third target, speed is set by the potentiometer
WaitTime=analogRead(SpeedPin) * WaitMultiplier;
delay(WaitTime);
//Retract the third target by spinning the third motor anti-clockwise
// Set the spinning direction for motor 3 anti-clockwise:
digitalWrite(M3dirPin, LOW);
// Spin the stepper motor 90 degrees:
for (int i = 0; i < StepsPerRevolution/4; i++) {
// These four lines result in 1 step as the stepper motor steps on the rising edge of a digital wave - Transition from Low to High:
digitalWrite(M3stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(M3stepPin, LOW);
delayMicroseconds(2000);
}
//Wait again before restarting the sequence, speed is set by the potentiometer
WaitTime=analogRead(SpeedPin) * WaitMultiplier;
delay(WaitTime);
}