// 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/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>
// Define a stepper and the pins it will use
AccelStepper stepper; // 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()
{  
  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();
}
Loading chip...chip-scope
Wokwi-stepper-motor AccelStepper.h demo.
Position 1023 CW CCW 0
Acceleration Fast Slow