/* Example sketch to control a stepper motor with A4988 stepper motor driver
and Arduino without a library. More info: https://www.makerguides.com */
// Define stepper motor connections and steps per revolution:
#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 200
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}
void loop() {
// Set the spinning direction clockwise:
int push = digitalRead(4);
int push2 = digitalRead(5);
if(push == 1){
digitalWrite(dirPin, HIGH);
// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
}
if(push2 == 1){
digitalWrite(dirPin, HIGH);
// Rotate for 2 Revolutions
for(int x = 0; x <2; x++){
for (int i = 0; i < stepsPerRevolution; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
}
}
delay(10);
}