// #include <AccelStepper.h>
// // Define pin numbers
// #define MOTOR1_STEP_PIN 2
// #define MOTOR1_DIR_PIN 3
// #define MOTOR2_STEP_PIN 4
// #define MOTOR2_DIR_PIN 5
// #define MOTOR3_STEP_PIN 6
// #define MOTOR3_DIR_PIN 7
// #define MOTOR4_STEP_PIN 8
// #define MOTOR4_DIR_PIN 9
// // Set the steps per revolution for your stepper motor
// #define STEPS_PER_REVOLUTION 200
// // Create an instance of the AccelStepper class for each motor
// AccelStepper motor1(AccelStepper::DRIVER, MOTOR1_STEP_PIN, MOTOR1_DIR_PIN);
// AccelStepper motor2(AccelStepper::DRIVER, MOTOR2_STEP_PIN, MOTOR2_DIR_PIN);
// AccelStepper motor3(AccelStepper::DRIVER, MOTOR3_STEP_PIN, MOTOR3_DIR_PIN);
// AccelStepper motor4(AccelStepper::DRIVER, MOTOR4_STEP_PIN, MOTOR4_DIR_PIN);
// void setup() {
// // Set the maximum speed and acceleration for each motor
// motor1.setMaxSpeed(1000.0);
// motor1.setAcceleration(500.0);
// motor2.setMaxSpeed(1000.0);
// motor2.setAcceleration(500.0);
// motor3.setMaxSpeed(1000.0);
// motor3.setAcceleration(500.0);
// motor4.setMaxSpeed(1000.0);
// motor4.setAcceleration(500.0);
// }
// void loop() {
// // Move each motor in a circular pattern for omnidirectional movement
// motor1.moveTo(STEPS_PER_REVOLUTION);
// motor2.moveTo(STEPS_PER_REVOLUTION);
// motor3.moveTo(STEPS_PER_REVOLUTION);
// motor4.moveTo(STEPS_PER_REVOLUTION);
// // Run the motors
// while (motor1.distanceToGo() != 0 || motor2.distanceToGo() != 0 || motor3.distanceToGo() != 0 || motor4.distanceToGo() != 0) {
// motor1.run();
// motor2.run();
// motor3.run();
// motor4.run();
// }
// // Pause before the next movement
// delay(1000);
// }
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;
const int joystickXPin = A0;
const int joystickYPin = A1;
void setup() {
// Initialize the stepper motor 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);
// Initialize the joystick pins as inputs
pinMode(joystickXPin, INPUT);
pinMode(joystickYPin, INPUT);
}
void loop() {
// Read the values of the joystick's x and y pins
int joystickXValue = analogRead(joystickXPin);
int joystickYValue = analogRead(joystickYPin);
// Map the joystick values to a range of -255 to 255
int motorSpeed1 = map(joystickXValue, 0, 1023, -255, 255);
int motorSpeed2 = map(joystickYValue, 0, 1023, -255, 255);
int motorSpeed3 = map(joystickXValue, 0, 1023, -255, 255);
int motorSpeed4 = map(joystickYValue, 0, 1023, -255, 255);
// Set the direction and speed of each stepper motor
if (motorSpeed1 > 0) {
digitalWrite(dirPin1, HIGH);
analogWrite(stepPin1, motorSpeed1);
} else {
digitalWrite(dirPin1, LOW);
analogWrite(stepPin1, -motorSpeed1);
}
if (motorSpeed2 > 0) {
digitalWrite(dirPin2, HIGH);
analogWrite(stepPin2, motorSpeed2);
} else {
digitalWrite(dirPin2, LOW);
analogWrite(stepPin2, -motorSpeed2);
}
if (motorSpeed3 > 0) {
digitalWrite(dirPin3, HIGH);
analogWrite(stepPin3, motorSpeed3);
} else {
digitalWrite(dirPin3, LOW);
analogWrite(stepPin3, -motorSpeed3);
}
if (motorSpeed4 > 0) {
digitalWrite(dirPin4, HIGH);
analogWrite(stepPin4, motorSpeed4);
} else {
digitalWrite(dirPin4, LOW);
analogWrite(stepPin4, -motorSpeed4);
}
}