// 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
// Code: https://wokwi.com/projects/327613078439985746
// derived from https://wokwi.com/projects/327324886912467538
// and: https://wokwi.com/projects/327379142347588180
// and: https://wokwi.com/projects/327381547863769683
// 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"
#include <AccelStepper.h>
#include <MultiStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepperR; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
AccelStepper stepperL(AccelStepper::FULL4WIRE,6,7,8,9); // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
// Up to 10 steppers can be handled as a group by MultiStepper
MultiStepper steppers;
// 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()
{
stepperR.setMaxSpeed(1000);
stepperR.setAcceleration(35);
stepperL.setMaxSpeed(1000*5);
stepperL.setAcceleration(35);
// Then give them to MultiStepper to manage
steppers.addStepper(stepperL);
steppers.addStepper(stepperR);
}
void loop()
{
// Read new position
long position = analogRead(PositionPot);
int accel = analogRead(AccelerationPot);
long positions[2]; // Array of desired stepper positions
positions[0] = 50 * position;
positions[1] = -0.5 * position;
stepperR.setAcceleration(accel);
stepperL.setAcceleration(accel);
steppers.moveTo(positions);
//steppers.runSpeedToPosition(); // Blocks until all are in position
steppers.run();
/*
stepperR.moveTo(position);
stepperR.run();
stepperL.moveTo(position*5);
stepperL.run();
*/
}