#include <AccelStepper.h>
// Define stepper motors and the pins they are connected to
AccelStepper stepper1(1, 9, 8); // STEP, DIR
AccelStepper stepper2(1, 3, 5); // Adjust pins as per your wiring
AccelStepper stepper3(1, 10, 11); // Adjust pins as per your wiring
AccelStepper stepper4(1, 12, 13); // Adjust pins as per your wiring
// Variables to control speed, acceleration, and number of steps for each motor
int maxSpeed = 100; // Maximum speed (steps per second)
int acceleration = 250; // Acceleration (steps per second^2)
int stepsPerRevolution = 200; // Number of steps per revolution (adjust based on your stepper motor)
int numRotationsForward = 10; // Number of rotations to move forward
int numRotationsBackward = 5; // Number of rotations to move backward
void setup() {
// Set up each stepper motor with max speed, acceleration, and initial position
setupMotor(stepper1);
setupMotor(stepper2);
setupMotor(stepper3);
setupMotor(stepper4);
// Move each stepper motor forward by the specified number of steps
moveStepper(stepper1, stepsPerRevolution * numRotationsForward);
// moveStepper(stepper2, stepsPerRevolution * numRotationsForward);
// moveStepper(stepper3, stepsPerRevolution * numRotationsForward);
// moveStepper(stepper4, stepsPerRevolution * numRotationsForward);
delay(1000); // Pause for 1 second
// Move each stepper motor backward by the specified number of steps
moveStepper(stepper1, stepsPerRevolution * (numRotationsForward - numRotationsBackward));
moveStepper(stepper2, stepsPerRevolution * (numRotationsForward - numRotationsBackward));
moveStepper(stepper3, stepsPerRevolution * (numRotationsForward - numRotationsBackward));
moveStepper(stepper4, stepsPerRevolution * (numRotationsForward - numRotationsBackward));
delay(1000); // Pause for 1 second
}
void loop() {
}
// Function to set up the motor parameters
void setupMotor(AccelStepper &stepper) {
stepper.setMaxSpeed(maxSpeed);
stepper.setAcceleration(acceleration);
stepper.setCurrentPosition(0); // Set initial position to 0
}
// Function to move the stepper motor to a specified position relative to its current position
void moveStepper(AccelStepper &stepper, int targetPosition) {
stepper.moveTo(targetPosition); // Set the target position relative to current position
stepper.runToPosition(); // Move to the target position with acceleration and deceleration
}