// 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};
//Use the array to represent the toggle status for 4 motors. Set the initial status for Motors 1-4
int TStatus[] {0,0,0,0};
#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);
Serial.begin(9600);
Serial.println("Initialising");
// 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
}
void loop() { //The code will repeat indefinitely
if(Serial.available()>0){
Serial.println("Waiting. . .");
Serial.end();
Wait(5000);
}
if (analogRead(A0)==LOW) {
Serial.println("Button 1");
if (TStatus[0]==0){ //If target retracted
PresentTarget(0); // Present Target
TStatus[0]=1; // Set the target status to '1' (Presented)
delay(50); //Delay to de-bounce the switch
} else { //Otherwise (i.e. The target is not retracted)
RetractTarget(0); //Retract the target
TStatus[0]=0; //Set the target status to '0' (Retracted)
delay(50); // Delay to de-bounce the switch
}
}
if (analogRead(A1)==LOW) {
if (TStatus[1]==0){
PresentTarget(1);
TStatus[1]=1;
delay(50);
} else {
RetractTarget(1);
TStatus[1]=0;
delay(50);
}
}
if (analogRead(A2)==LOW) {
if (TStatus[2]==0){
PresentTarget(2);
TStatus[2]=1;
delay(50);
} else {
RetractTarget(2);
TStatus[2]=0;
delay(50);
}
}
if (analogRead(A3)== LOW ){
if (TStatus[3]==0){
PresentTarget(3);
TStatus[3]=1;
delay(50);
} else {
RetractTarget(3);
TStatus[3]=0;
delay(50);
}
}
}
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);
}
}