#include "AccelStepper.h"

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::FULL4WIRE, 2, 3, 4, 5);

// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0

// Define a scaling factor to convert analog input range to suitable range for stepper
#define SCALING_FACTOR 5

void setup() {  
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(500); // Adding acceleration to smooth stepper motor operation
}

void loop() {
  // Read new position
  int analog_in = analogRead(ANALOG_IN);

  // Convert analog input to a suitable range for stepper
  long newPosition = analog_in * SCALING_FACTOR;

  stepper.moveTo(newPosition);
  // No need to setSpeed here as moveTo manages the speed automatically

  // Run the motor to the new position
  while (stepper.distanceToGo() != 0) {
    stepper.run();
  }
}
Loading chip...chip-scope