// 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();
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
sw1:A-
sw1:A+
sw1:B+
sw1:B-
pot1:VCC
pot1:SIG
pot1:GND
pot2:VCC
pot2:SIG
pot2:GND
ScopeBreakout
scope1:D0
scope1:D1
scope1:D2
scope1:D3
scope1:A3
scope1:A2
scope1:A1
scope1:A0
Wokwi-stepper-motor AccelStepper.h demo.
Position 1023 CW CCW 0
Acceleration Fast Slow