#include <Stepper.h>
// Define the number of steps per revolution for each stepper motor
const int stepsPerRevolution1 = 200; // Change this value based on your motor's specifications
const int stepsPerRevolution2 = 200; // Change this value based on your motor's specifications
// Create stepper motor objects
Stepper stepper1(stepsPerRevolution1, 14, 12, 13, 15); // Pins: STEP, DIR, MS1, MS2
Stepper stepper2(stepsPerRevolution2, 5, 18, 19, 21); // Pins: STEP, DIR, MS1, MS2
// Define the angles (in degrees)
const int initialAngle1 = 0; // Start at 0 degrees
const int targetAngle1 = 300;
const int initialAngle2 = 0; // Start at 0 degrees
const int targetAngle2 = 180;
// Define the duration for both movements (in milliseconds)
const unsigned long movementDuration = 5000; // 5 seconds per movement
// Track the current position of each stepper
int currentPos1 = 0;
int currentPos2 = 0;
void setup() {
// Set the speed of both stepper motors
stepper1.setSpeed(100); // Adjust the speed as needed
stepper2.setSpeed(100); // Adjust the speed as needed
}
void loop() {
unsigned long startTime = millis();
while (millis() - startTime < movementDuration) {
float progress = (millis() - startTime) / (float)movementDuration;
// Calculate the current angle for both steppers
int currentAngle1 = initialAngle1 + (targetAngle1 - initialAngle1) * progress;
int currentAngle2 = initialAngle2 + (targetAngle2 - initialAngle2) * progress;
// Calculate the number of steps for both steppers
int steps1 = currentAngle1 * stepsPerRevolution1 / 360;
int steps2 = currentAngle2 * stepsPerRevolution2 / 360;
// Move both steppers relative to their current positions
stepper1.step(steps1 - currentPos1);
stepper2.step(steps2 - currentPos2);
// Update the current positions for both steppers
currentPos1 = steps1;
currentPos2 = steps2;
}
// Delay before moving the next set of movements
delay(1000);
// Return both steppers to the initial position (0 degrees)
stepper1.step(-currentPos1);
stepper2.step(-currentPos2);
// Reset the current positions to zero for both steppers
currentPos1 = 0;
currentPos2 = 0;
}