// https://wokwi.com/projects/408686707799390209
// ProportionalControlAccelStepperEncoder.ino
// Reads the Wokwi's stepper quadrature inputs (pins 3&4) as 
// encoder quadrature outputs from an encoder.
// daveX 2024
// 

// ProportionalControl.pde
// -*- mode: C++ -*-
//
// Make a single stepper follow the analog value read from a pot or whatever
// The stepper will move smoothly using acceleration to each newly set posiiton,
// depending on the value of the pot.
//
// Copyright (C) 2012 Mike McCauley
// $Id: ProportionalControl.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $

// Code derived from: 
// https://www.airspayce.com/mikem/arduino/AccelStepper/ProportionalControl_8pde-example.html
// Sim https://wokwi.com/projects/408621270297541633
// derived from https://wokwi.com/projects/327381547863769683
//  derived from https://wokwi.com/projects/327324886912467538
//         and:  https://wokwi.com/projects/327379142347588180
// Discussion: https://discord.com/channels/787627282663211009/787630013658824707/957769150556667954
//         and https://github.com/wokwi/wokwi-features/issues/191
// Docs:  https://docs.wokwi.com/parts/wokwi-stepper-motor
//    note the wokwi-stepper-motor attributes in diagram.json:
//   "attrs": { "display": "steps", "arrow": "white", "gearRatio": "200:1023"
//
//  See also: https://wokwi.com/projects/327613078439985746 MultiStepper
//            https://wokwi.com/projects/330649945363186260 stepper pot, and servo
//
#include <AccelStepper.h> // https://www.airspayce.com/mikem/arduino/AccelStepper/index.html
#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html

Encoder enc(3, 4);

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER,9,8); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 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
const byte PositionPot = A0;
const byte AccelerationPot = A1;
void setup()
{
  Serial.begin(115200);
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(35);
}
void loop()
{
  // Read new position
  int analog_in = analogRead(PositionPot);
  stepper.setAcceleration(analogRead(AccelerationPot));
  stepper.moveTo(analog_in);
  stepper.run();
  //enc.read(); // 3 is an interrupt pin, so unnecessary
  report();
}

void report() {
  const uint32_t interval = 100;
  static uint32_t last = -interval;
  static long lastEnc = 0;
  uint32_t now = millis();
  if (now - last >= interval) {
    last = now;
    long nowEnc = enc.read();
    if (nowEnc != lastEnc) {
      lastEnc = nowEnc;
      Serial.print("Encoder:");
      Serial.print(enc.read());
      Serial.println();
    }
  }
}
ScopeBreakout
Wokwi-stepper-motor A4988 Driver Encoder Sim AccelStepper.h demo. Move the posioton and acceleration sliders.
Position 1023 CW CCW 0
Acceleration Fast Slow
A4988