// https://wokwi.com/projects/428502497518500865
//
// modified to use a driver. Based on:
// https://wokwi.com/projects/410058296261374977
// Code from https://www.airspayce.com/mikem/arduino/AccelStepper/ProportionalControl_8pde-example.html
// Other example simulations https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754
// See also https://wokwi.com/projects/408621270297541633 with acceleration
//
// Driver example
//
// AccelStepper::DRIVER initialization parameter like:
// AccelStepper stepper(AccelStepper::DRIVER,4,3);
// This simulation shares both wirings so the code
// distributed with AccelStepper works as-is
//
//
// ProportionalControlV2.pde
// -*- mode: C++ -*-
//
// Make a single stepper follow the analog value read from a pot or whatever
// The stepper will move with acceleration to each newly set posiiton,
// depending on the value of the pot, or in Speed mode, move CW or CCW at
// variable speed,
//
// Copyright (C) 2012 Mike McCauley
// $Id: ProportionalControl.pde,v 1.1 2011/01/05 01:51:01 mikem Exp mikem $
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 4, 3);
// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0
#define MODE_PIN A1
void setup()
{
stepper.setMaxSpeed(1000); // steps/sec
stepper.setAcceleration(100); // steps/sec/sec
pinMode(MODE_PIN, INPUT_PULLUP);
Serial.begin(115200);
}
void loop()
{
static int last_analog_in = -1, last_mode = -1;
int analog_in = analogRead(ANALOG_IN);
int mode = digitalRead(MODE_PIN)==HIGH;
bool inputs_changed = false;
if (abs(analog_in - last_analog_in) > 2 || mode != last_mode ) {
// change detection on inputs to limit printing and readjustments
inputs_changed = true;
last_analog_in = analog_in;
last_mode = mode;
}
if (mode == HIGH) { // Position mode
if (inputs_changed) { // Set new position
//stepper.setSpeed(100);
stepper.moveTo(analog_in);
Serial.print("moving to:");
Serial.println(analog_in);
}
stepper.run(); // move stepper as needed
} else {
if (inputs_changed) { // Set new speed
float new_speed = map(analog_in, 0, 1023, -1000, 1000) / 10.0;
stepper.setSpeed(new_speed);
Serial.print("New speed:");
Serial.print(new_speed);
Serial.println(" steps/sec");
}
stepper.runSpeed(); // move stepper as needed
}
}
Wokwi-stepper-motor
AccelStepper.h demo.
Position
1023
CW
CCW
0
Speed
100CW
100 CCW
Position <> Speed