#include <AccelStepper.h>
// Define the connections for the A4988 stepper motor driver
#define dirPin 2
#define stepPin 15
#define stepsPerRevolution 200 // Change this according to your stepper motor's specifications
AccelStepper stepper(1, stepPin, dirPin); // Initialize the stepper library
void setup() {
Serial.begin(9600);
stepper.setMaxSpeed(1000); // Adjust the maximum speed of your stepper motor
stepper.setAcceleration(500); // Adjust the acceleration of your stepper motor
}
void loop() {
for (int i = 0; i < 10; i++) { // Repeat the cycle 10 times
rotateClockwise();
delay(1000); // Delay after rotating clockwise
rotateAntiClockwise();
delay(1000); // Delay after rotating anticlockwise
}
}
void rotateClockwise() {
stepper.moveTo(stepsPerRevolution / 10); // Rotate 36 degrees (10% of a full revolution) clockwise
stepper.run();
}
void rotateAntiClockwise() {
stepper.moveTo(-stepsPerRevolution / 10); // Rotate 36 degrees (10% of a full revolution) anticlockwise
stepper.run();
}