// 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 <LiquidCrystal_I2C.h>
#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;
const byte SpeedPot = A2;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void lcd_display(long curr, int speed, int accel, int xval){
char str[30];
sprintf(str,"a:%4dpos:%4d",accel,xval);
lcd.setCursor(0, 1);
lcd.print(str);
sprintf(str, "s:%4dcurr:%6.6ld",speed, curr);
lcd.setCursor(0, 0);
lcd.print(str);
/*
lcd.print("acc:");
lcd.print(accel);
lcd.setCursor(10, 1);
lcd.print("Pos:");
lcd.print(xval);
*/
}
void lcd_setup(){
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("AccelStepper");
}
void setup()
{
stepper.setMaxSpeed(1000);
stepper.setAcceleration(35);
lcd_setup();
}
void loop()
{
// Read new position
int analog_in = analogRead(PositionPot);
int accel_in = analogRead(AccelerationPot);
int speed_in = analogRead(SpeedPot);
long current = stepper.currentPosition();
stepper.setSpeed(speed_in);
stepper.setAcceleration(accel_in);
stepper.moveTo(analog_in);
stepper.run();
lcd_display(current, speed_in, accel_in, analog_in);
}