#include <AccelStepper.h>
// Define the number of steps per revolution for your stepper motor
const int stepsPerRevolution = 20;
// Joystick input pins
const int joystickXPin = A0; // Joystick X-axis input
const int joystickYPin = A1; // Joystick Y-axis input
// Motor driver pins
const int motorStepPin = 2; // Step pin of the motor driver
const int motorDirPin = 3; // Direction pin of the motor driver
// Create an instance of the AccelStepper class
AccelStepper stepper(AccelStepper::DRIVER, motorStepPin, motorDirPin);
void setup() {
// Set the maximum speed and acceleration of the stepper motor
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
// Initialize the joystick position
int joystickX = analogRead(joystickXPin);
int joystickY = analogRead(joystickYPin);
}
void loop() {
// Read the joystick input values
int joystickX = analogRead(joystickXPin);
int joystickY = analogRead(joystickYPin);
// Map the joystick input values to the motor speed and direction
int motorSpeed = map(joystickY, 0, 1023, 0, 1000);
int motorDirection = (joystickX < 512) ? -1 : 1;
// Set the motor speed and direction
stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorSpeed * 2); // Optional: Adjust acceleration based on speed
stepper.move(motorDirection * stepsPerRevolution);
// Update the stepper motor position
stepper.run();
// Delay for stability
delay(10);
}