#include <Servo.h>
#include <AccelStepper.h>
// Servo objects
Servo servo1; // Controlled by potentiometer 1
Servo servo2; // Controlled by potentiometer 1 (opposite direction)
Servo servo3; // Controlled by potentiometer 2
Servo servo4; // Controlled by potentiometer 3
Servo servo5; // Controlled by potentiometer 4
Servo servo6; // Controlled by potentiometer 5
// Define potentiometer pins for servos
const int potPin1 = A1; // Potentiometer for servo1 and servo2
const int potPin2 = A2; // Potentiometer for servo3
const int potPin3 = A3; // Potentiometer for servo4
const int potPin4 = A4; // Potentiometer for servo5
const int potPin5 = A5; // Potentiometer for servo6
// Stepper motor connections and motor interface type
#define STEP_PIN 2
#define DIR_PIN 4
// Create an instance of the AccelStepper class
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// Potentiometer pin for stepper motor
const int potPinStepper = A0;
// New potentiometer for controlling stepper motor direction limits
const int potPinDirection = A6; // Potentiometer for direction control
// Define the maximum speed for your stepper motor
const int maxStepperSpeed = 1000; // Adjust this value as needed
void setup() {
// Attach servos to their respective pins
servo1.attach(3); // Servo 1
servo2.attach(5); // Servo 2
servo3.attach(6); // Servo 3
servo4.attach(9); // Servo 4
servo5.attach(10); // Servo 5
servo6.attach(11); // Servo 6
// Set the maximum speed and acceleration for the stepper motor
stepper.setMaxSpeed(maxStepperSpeed);
stepper.setAcceleration(500); // Adjust as needed
}
void loop() {
// Read the potentiometer values for servos
int potValue1 = analogRead(potPin1);
int potValue2 = analogRead(potPin2);
int potValue3 = analogRead(potPin3);
int potValue4 = analogRead(potPin4);
int potValue5 = analogRead(potPin5);
// Map the potentiometer values to servo angles (0-180 degrees)
int angle1 = map(potValue1, 0, 1023, 0, 180);
int angle2 = map(potValue1, 0, 1023, 180, 0); // Opposite direction for servo2
int angle3 = map(potValue2, 0, 1023, 0, 180);
int angle4 = map(potValue3, 0, 1023, 0, 180);
int angle5 = map(potValue4, 0, 1023, 0, 180);
int angle6 = map(potValue5, 0, 1023, 0, 180);
// Set the servo angles
servo1.write(angle1);
servo2.write(angle2);
servo3.write(angle3);
servo4.write(angle4);
servo5.write(angle5);
servo6.write(angle6);
// Read the potentiometer value for the stepper motor
int potValueStepper = analogRead(potPinStepper);
// Read the potentiometer value for direction control
int potValueDirection = analogRead(potPinDirection);
// Determine the target speed based on the stepper potentiometer
int targetSpeed = map(potValueStepper, 0, 1023, -maxStepperSpeed*2, maxStepperSpeed*2);
// Check if the potentiometer value for the stepper motor is between 400 and 600
if (potValueStepper >= 450 && potValueStepper <= 550) {
targetSpeed = 0; // Stop the motor if the value is in the range
}
// Control the direction based on the new potentiometer value
if (potValueDirection >= 1000) {
// Disable right rotation
if (targetSpeed > 0) {
targetSpeed = 0; // Stop the motor if trying to spin right
}
} else if (potValueDirection < 20) {
// Disable left rotation
if (targetSpeed < 0) {
targetSpeed = 0; // Stop the motor if trying to spin left
}
}
// Set the speed of the stepper motor
stepper.setSpeed(targetSpeed);
// Run the stepper motor at the set speed
stepper.runSpeed();
// Add a small delay to allow the servos to move
delay(15);
}