#include <Servo.h>
// Define pins for stepper motor driver connections
const int stepPin1 = 2;
const int dirPin1 = 3;
const int stepPin2 = 4;
const int dirPin2 = 5;
const int stepPin3 = 6;
const int dirPin3 = 7;
const int stepPin4 = 8;
const int dirPin4 = 9;
// Define pins for servo motor
const int servoPin = 10;
// Define some constants
const int stepsPerRevolution = 200; // Steps per revolution for your stepper motor
const int servoMin = 0; // Minimum angle for the servo
const int servoMax = 180; // Maximum angle for the servo
// Create servo object
Servo myServo;
void setup() {
// Set the pins as outputs
pinMode(stepPin1, OUTPUT);
pinMode(dirPin1, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(stepPin3, OUTPUT);
pinMode(dirPin3, OUTPUT);
pinMode(stepPin4, OUTPUT);
pinMode(dirPin4, OUTPUT);
// Attach servo to pin
myServo.attach(servoPin);
}
void loop() {
// Move stepper motor 1
moveStepper(stepPin1, dirPin1);
// Move stepper motor 2
moveStepper(stepPin2, dirPin2);
// Move stepper motor 3
moveStepper(stepPin3, dirPin3);
// Move stepper motor 4
moveStepper(stepPin4, dirPin4);
// Move servo
moveServo();
}
// Function to move stepper motor
void moveStepper(int stepPin, int dirPin) {
// Set direction
digitalWrite(dirPin, HIGH); // Set direction to clockwise (or counterclockwise if needed)
// Step the motor
for (int i = 0; i < stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500); // Adjust as needed for your motor speed
digitalWrite(stepPin, LOW);
delayMicroseconds(500); // Adjust as needed for your motor speed
}
// Reverse direction after completing a full revolution
digitalWrite(dirPin, LOW); // Set direction to counterclockwise (or clockwise if needed)
// Step the motor back to original position
for (int i = 0; i < stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500); // Adjust as needed for your motor speed
digitalWrite(stepPin, LOW);
delayMicroseconds(500); // Adjust as needed for your motor speed
}
}
// Function to move servo motor
void moveServo() {
// Move the servo from min to max angle
for (int angle = servoMin; angle <= servoMax; angle++) {
myServo.write(angle);
delay(15); // Adjust as needed for servo speed
}
// Move the servo from max to min angle
for (int angle = servoMax; angle >= servoMin; angle--) {
myServo.write(angle);
delay(15); // Adjust as needed for servo speed
}
}