#include <AccelStepper.h>
// Define the pins for the stepper motor
#define DIR_PIN 7
#define STEP_PIN 4
// Define the analog input pins for speed and acceleration control
const int SPEED_POTI_PIN = A3; // Connect the potentiometer for speed control to this pin
const int ACCELERATION_POTI_PIN = A4; // Connect the potentiometer for acceleration control to this pin
// Create an instance of the AccelStepper class
AccelStepper stepper(AccelStepper::FULL4WIRE, DIR_PIN, STEP_PIN);
void setup() {
// Set the initial position (optional)
stepper.setCurrentPosition(0);
// Set the direction (1 for clockwise, -1 for counterclockwise)
stepper.setSpeed(0); // Set the initial speed to 0
// Set the maximum speed and acceleration (you can adjust these values)
stepper.setMaxSpeed(1000); // Maximum steps per second
stepper.setAcceleration(500); // Initial acceleration value
}
void loop() {
// Read the potentiometer values for speed and acceleration
int speedPotiValue = analogRead(SPEED_POTI_PIN);
int accelerationPotiValue = analogRead(ACCELERATION_POTI_PIN);
// Map the potentiometer values to the desired speed and acceleration ranges
int speed = map(speedPotiValue, 0, 1023, 0, 1000);
int acceleration = map(accelerationPotiValue, 0, 1023, 1, 1000);
// Read the joystick value to control direction (for example, using A2)
int joystickValue = analogRead(A2);
int direction = (joystickValue > 512) ? 1 : -1;
// Set the speed, direction, and acceleration for the stepper motor
stepper.setSpeed(speed * direction);
stepper.setAcceleration(acceleration);
// Move the stepper motor continuously based on the joystick input
stepper.runSpeed();
}