// 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 between presenting and retracting targets
//Define the pin to enable the motors
int EnPin = 8;
//Set Direction Pins for Motors 1-4 (Using an array so they are counted as 0-3 rather than 1-4)
int DirPins[] {5,6,7,13};
//Set Step Pints for Motors 1-4 (Using an array so they are counted as 0-3 rather than 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
pinMode(LED_BUILTIN, OUTPUT);
// 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
// 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 10 targets by randomly choosing one of the four possible targets each time
for (int i = 1; i <=10; i++) {
int TargetNumber = random(4);
PresentTarget(TargetNumber) ;
delay (WaitTime);
RetractTarget (TargetNumber) ;
delay (WaitTime);
}
}
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); //Look up the direction pin for the current motor in the direction pins array (DirPins) and set it HIGH for clockwise motion
// 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); //Look up the step pin for the current motor in the step pins array (StepPins) and set it HIGH to create a rising edge on a square wave.
delayMicroseconds(2000); //Wait 2000 microseconds
digitalWrite(StepPins[TargetNumber], LOW); //Look up the step pin for the current motor in the step pins array (StepPins) and set it LOW to create a falling edge on a square wave.
delayMicroseconds(2000); //Wait 2000 microseconds
}
}
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); //Look up the direction pin for the current motor in the direction pins array (DirPins) and set it LOW for anti-clockwise motion
// 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); //Look up the step pin for the current motor in the step pins array (StepPins) and set it HIGH to create a rising edge on a square wave.
delayMicroseconds(2000); //Wait 2000 microseconds
digitalWrite(StepPins[TargetNumber], LOW); //Look up the step pin for the current motor in the step pins array (StepPins) and set it LOW to create a falling edge on a square wave.
delayMicroseconds(2000); //Wait 2000 microseconds
}
}