#include AF_Stepper motorX(200, 1); // 200 steps per revolution, motor port 1 AF_Stepper motorY(200, 2); // 200 steps per revolution, motor port 2 const int xAxisPin = A0; const int yAxisPin = A1; const int joystickThreshold = 50; // Adjust the threshold as needed void setup() { Serial.begin(9600); Serial.println("AFMotor Library - Joystick Control"); motorX.setSpeed(300); motorY.setSpeed(300); pinMode(xAxisPin, INPUT); pinMode(yAxisPin, INPUT); } void loop() { int xValue = analogRead(xAxisPin); int yValue = analogRead(yAxisPin); int motorSpeedX = map(xValue, 0, 1023, -255, 255); int motorSpeedY = map(yValue, 0, 1023, -255, 255); // Control motor X with the X-axis controlStepper(&motorX, motorSpeedX); // Control motor Y with the Y-axis controlStepper(&motorY, motorSpeedY); delay(10); } void controlStepper(AF_Stepper *stepper, int speed) { if (abs(speed) > joystickThreshold) { stepper->setSpeed(abs(speed)); // Set the speed based on joystick input magnitude if (speed > 0) { stepper->step(1, FORWARD, SINGLE); // 1 step, FORWARD direction } else { stepper->step(1, BACKWARD, SINGLE); // 1 step, BACKWARD direction } } else { stepper->release(); // Release the motor if joystick input is below the threshold } }