#include <AccelStepper.h>
// Define the pins for the stepper motor
#define STEP_PIN 2
#define DIR_PIN 3
// Define the analog pin for the potentiometer
#define POTENTIOMETER_PIN A0
// Create an instance of the AccelStepper class
AccelStepper stepper(1, STEP_PIN, DIR_PIN);
void setup() {
// Set the maximum speed and acceleration of the stepper motor
stepper.setMaxSpeed(1000); // Adjust this value as needed
stepper.setAcceleration(500); // Adjust this value as needed
// Initialize the serial communication
Serial.begin(9600);
}
void loop() {
// Read the value from the potentiometer (0-1023)
int potValue = analogRead(POTENTIOMETER_PIN);
// Map the potentiometer value to the desired stepper motor position (0-2000 steps)
int targetPosition = map(potValue, 0, 1023, 0, 2000);
// Move the stepper motor to the target position
stepper.moveTo(targetPosition);
// Run the stepper motor
stepper.run();
// Print the current potentiometer and stepper positions
Serial.print("Potentiometer: ");
Serial.print(potValue);
Serial.print(" | Stepper Position: ");
Serial.println(stepper.currentPosition());
delay(10); // Optional delay to control the update rate
}