/*
   MotorKnob

   A stepper motor follows the turns of a potentiometer
   (or other sensor) on analog input 0.

   https://docs.arduino.cc/learn/electronics/stepper-motors
   This example code is in the public domain.
*/

#include <Stepper.h>

// change this to the number of steps on your motor
const int STEPS = 800;
const int STEP_PIN = 3;
const int DIR_PIN = 2;

// create an instance of the Stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, STEP_PIN, DIR_PIN);

// the previous reading from the analog input
int previous = 0;

void setup() {
  Serial.begin(9600);
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(30);
}

void loop() {
  // get the sensor value
  int val = analogRead(0);
  int mappedVal = map(val, 0, 1023, 0, STEPS);
  Serial.print("Mapped value: ");
  Serial.println(mappedVal);

  // move a number of steps equal to the change in the
  // sensor reading
  stepper.step(mappedVal - previous);

  // remember the previous value of the sensor
  previous = mappedVal;
}
A4988