// 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 stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepper2(AccelStepper::FULL4WIRE, 8, 9, 10, 11);
// 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()
{
stepper1.setMaxSpeed(1000);
stepper1.setAcceleration(30);
stepper2.setMaxSpeed(1000);
stepper2.setAcceleration(10);
Serial.begin(115200);
}
void loop()
{
// Read new position
int analog_in = analogRead(PositionPot);
stepper1.setAcceleration(analogRead(AccelerationPot));
stepper1.moveTo(analog_in);
stepper1.run();
int analog_in2 = analogRead(PositionPot)/2;
stepper2.setAcceleration(analogRead(AccelerationPot));
stepper2.moveTo(analog_in2);
stepper2.run();
}