// 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 Direction Pins for Motors 1-4
int DirPins[] {5,6,7,13};
//Set Step Pints for Motors 1-4
int StepPins[] {2,3,4,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 as output
pinMode(LED_BUILTIN, OUTPUT);
// Set the button pins as input
pinMode(A0,INPUT_PULLUP);
pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
pinMode(A3,INPUT_PULLUP);
// Declare motor pins as output:
for (int i = 0; i <= 3 ; i++) {
pinMode (DirPins[i], OUTPUT);
pinMode (StepPins[i], 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
Serial.println("Looping");
if (analogRead(A0)==LOW) {
Serial.println("Button 1 Pressed");
PresentTarget(0);
delay(2000);
RetractTarget(0);
}
if (analogRead(A1)==LOW) {
Serial.println("Button 2 Pressed");
PresentTarget(1);
delay(2000);
RetractTarget(1);
}
if (analogRead(A2)==LOW) {
Serial.println("Button 3 Pressed");
PresentTarget(2);
delay(2000);
RetractTarget(2);
}
if (analogRead(A3)== LOW ){
Serial.println("Button 4 Pressed");
PresentTarget(3);
delay(2000);
RetractTarget(3);
}
}
void PresentTarget (int TargetNumber){
//Present the selected target by spinning the relevant motor clockwise
// Set the spinning direction for the selected motor to clockwise:
digitalWrite(DirPins[TargetNumber], 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(StepPins[TargetNumber], HIGH);
delayMicroseconds(1000);
digitalWrite(StepPins[TargetNumber], LOW);
delayMicroseconds(1000);
}
}
void RetractTarget (int TargetNumber){
//Retract the selected target by spinning the relevant motor clockwise
// Set the spinning direction for the selected motor to clockwise:
digitalWrite(DirPins[TargetNumber], 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(StepPins[TargetNumber], HIGH);
delayMicroseconds(1000);
digitalWrite(StepPins[TargetNumber], LOW);
delayMicroseconds(1000);
}
}