// Example sketch to control a stepper motor with A4988 stepper motor driver
// and Arduino without a library.
// More info: https://www.makerguides.com
// Define a default wait time
int WaitTime = 2000; //Set a default wait time of 2000ms / 2 seconds
//Define the enable pin
int EnPin = 8;
//Set Step and Direction Pins for Motor 1
int M1DirPin=5;
int M1StepPin = 2;
//Set Step and Direction Pins for Motor 2
int M2DirPin = 6;
int M2StepPin = 3;
//Set Step and Direction Pins for Motor 3
int M3DirPin = 7;
int M3StepPin = 4;
//Set Step and Direction Pins for Motor 4
int M4DirPin = 13;
int M4StepPin = 12;
#define StepsPerRevolution 200 //Assumes that all motors are the same and have 200 steps per revolution / 1.8 degree steps
void setup() {
// Set the inbuilt LED
pinMode(LED_BUILTIN, OUTPUT);
// Declare motor pins as output:
pinMode(M1StepPin, OUTPUT);
pinMode(M1DirPin, OUTPUT);
pinMode(M2StepPin, OUTPUT);
pinMode(M2DirPin, OUTPUT);
pinMode(M3StepPin, OUTPUT);
pinMode(M3DirPin, OUTPUT);
pinMode(M4StepPin, OUTPUT);
pinMode(M4DirPin, OUTPUT);
//Enable the motors
digitalWrite(EnPin, LOW);
// The built in LED will flash twice quickly during initial setup
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for half a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for half a second
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for half a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for half a second
Serial.begin(9600);
}
void loop() { //The code will repeat indefinitely
// The built in LED will flash each time the sequence restarts
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
//Present the first target by spinning the first motor clockwise
Serial.println("Present Target 1");
PresentTarget(1);
//Wait before retracting the target
WaitTime=1000;
delay(WaitTime);
Serial.println("Retract Target 1");
RetractTarget (1);
//Wait before presenting the second target
delay(WaitTime);
//Present the second target by spinning the second motor clockwise
Serial.println("Present Target 2");
PresentTarget(2);
//Wait before retracting the second target
delay(WaitTime);
//Retract the second target by spinning the second motor anti-clockwise
Serial.println("Retract Target 2");
RetractTarget(2);
//Wait before presenting the third target
delay(WaitTime);
//Present the third target by spinning the third motor clockwise
Serial.println("Present Target 3");
PresentTarget(3);
//Wait before retracting the third target
delay(WaitTime);
//Retract the third target by spinning the third motor anti-clockwise
Serial.println("Retract Target 3");
RetractTarget (3);
//Wait before presenting the fourth target
delay(WaitTime);
//Present the fourth target by spinning the fourth motor clockwise
Serial.println("Present Target 4");
PresentTarget (4);
//Wait before retracting the fourth target
delay(WaitTime);
//Retract the fourth target by spinning the fourth motor anti-clockwise
Serial.println("Retract Target 4");
RetractTarget (4);
//Wait again before restarting the sequence, speed is set by the potentiometer
delay(WaitTime);
}
void PresentTarget (int TargetNumber){
int MotorDirPin;
int MotorStepPin;
Serial.println ("Presenting Target");
Serial.println(TargetNumber);
switch (TargetNumber) {
case 1:{
MotorDirPin=M1DirPin;
MotorStepPin=M1StepPin;
}
break;
case 2: {
MotorDirPin=M2DirPin;
MotorStepPin=M2StepPin;
}
break;
case 3: {
MotorDirPin=M3DirPin;
MotorStepPin=M3StepPin;
}
case 4: {
MotorDirPin=M4DirPin;
MotorStepPin=M4StepPin;
}
default:{}
break;
}
Serial.println("Using Motors. . .");
Serial.println(MotorDirPin);
Serial.println(MotorStepPin);
//Present the selected target by spinning the relevant motor clockwise
// Set the spinning direction for the selected motor to clockwise:
digitalWrite(MotorDirPin, 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(MotorStepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(MotorStepPin, LOW);
delayMicroseconds(2000);
}
}
void RetractTarget (int TargetNumber){
int MotorDirPin;
int MotorStepPin;
Serial.println ("Retracting Target");
Serial.println(TargetNumber);
switch (TargetNumber) {
case 1:{
MotorDirPin=M1DirPin;
MotorStepPin=M1StepPin;
}
break;
case 2:{
MotorDirPin=M2DirPin;
MotorStepPin=M2StepPin;
}
break;
case 3:{
MotorDirPin=M3DirPin;
MotorStepPin=M3StepPin;
}
break;
case 4:{
MotorDirPin=M4DirPin;
MotorStepPin=M4StepPin;
}
default:
break;
}
Serial.println ("Using Motors...");
Serial.println(MotorDirPin);
Serial.println(MotorStepPin);
//Retract the selected target by spinning the relevant motor clockwise
// Set the spinning direction for the selected motor to clockwise:
digitalWrite(MotorDirPin, 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(MotorStepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(MotorStepPin, LOW);
delayMicroseconds(2000);
}
}