// 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[] {10};
//Set Step Pints for Motors 1-4
int StepPins[] {9};
#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(3, INPUT_PULLUP);
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
// Declare motor pins as output:
for (int i = 0; i <= 0 ; 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 (digitalRead(3)==LOW) {
Serial.println("Button 1 Pressed");
PresentTarget(0);
delay(1000);
RetractTarget(0);
}
if (digitalRead(4)==LOW) {
Serial.println("Button 2 Pressed");
PresentTarget(0);
delay(2000);
RetractTarget(0);
}
if (digitalRead(5)==LOW) {
Serial.println("Button 3 Pressed");
PresentTarget(0);
delay(3000);
RetractTarget(0);
}
if (digitalRead(6)== LOW ){
Serial.println("Button 4 Pressed");
PresentTarget(0);
delay(4000);
RetractTarget(0);
}
}
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);
}
}