Knob control using the accelstepper library

1,600 views
Skip to first unread message

Rob_R

unread,
May 23, 2012, 6:38:51 PM5/23/12
to accelstepper
Hi!

I'm a fair noob to Arduino programming, but catching on fairly
quickly. I'm using an EasyDriver with the accelstepper library to
control a stepper motor, which controls a three-sided theatrical set
piece. I currently have it coded to be controlled with three buttons,
each to a digital pin, each of which turn the piece 120° to the side
that corresponds to its button. It also sends its current position to
an LCD screen on the control box. All of that is running fine. (I'm
trying to make it as simple as possible, to avoid operator error.) The
final bit I want to add is a 10K pot for fine tuning. Hopefully
unnecessary, but just in case something happens and the piece needs to
be tweeked a little so that it is in its proper orientation.

I've been trying to work with analogRead() and stepper.move(), but my
knowledge of the move() function in this library is incredibly
limited. Alright, nonexistent. I was able to make it happen with the
original Stepper library, but I prefer the accelstepper library. I was
basing it on this sketch:

void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30); //**This is established in the setMaxSpeed
with accelstepper?
}

void loop()
{
// get the sensor value
int val = analogRead(0);

// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous); //Does this become stepper.move(val -
previous);?

// remember the previous value of the sensor
previous = val;
}

Any help or thoughts anyone might have would be greatly appreciated!
Cheers
Rob

Mike McCauley

unread,
May 23, 2012, 7:10:40 PM5/23/12
to accels...@googlegroups.com
Hi Rob,

you will need something like this:

Ive added this as a sample for the next version.


#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to 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
#define ANALOG_IN A0

void setup()
{
stepper.setMaxSpeed(1000);
}

void loop()
{
// Read new position
int analog_in = analogRead(ANALOG_IN);
stepper.moveTo(analog_in);
stepper.setSpeed(100);
stepper.runSpeedToPosition();
}


Cheers
--
Mike McCauley mi...@open.com.au
Open System Consultants Pty. Ltd
9 Bulbul Place Currumbin Waters QLD 4223 Australia http://www.open.com.au
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

Rob_R

unread,
May 25, 2012, 6:32:11 PM5/25/12
to accelstepper

Hi Mike -

Thank you so much, I really appreciate the help. Once I understood the
syntax that I was lacking, I was able to modify it slightly so that it
fit my needs perfectly. I have been attempting to make this project
as, well, idiot-proof as possible, as I don't know who will be running
it, and I couldn't have done it without your help.

Thanks again for the help, and for this great library!

Cheers!

Rob

Mike McCauley

unread,
May 25, 2012, 6:42:42 PM5/25/12
to accels...@googlegroups.com
Hi,

thanks.
And just for completeness in the email record, here is a version that does
accelerations as well:

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to 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
#define ANALOG_IN A0

void setup()
{
stepper.setMaxSpeed(100);
stepper.setAcceleration(100);
}

void loop()
{
// Read new position
static int last_analog_in;
int analog_in = analogRead(ANALOG_IN);
if (analog_in != last_analog_in)
{
stepper.moveTo(analog_in);
last_analog_in = analog_in;
}
stepper.run();
}

Cheers.

Lim Gabriel

unread,
Mar 9, 2017, 9:40:43 AM3/9/17
to accelstepper
Hi friends, i cant make the library you created work, please help?:(

gregor

unread,
Mar 9, 2017, 10:27:41 AM3/9/17
to accelstepper
you need to provide more info, and your code.

Lim Gabriel

unread,
Mar 9, 2017, 11:03:59 AM3/9/17
to accelstepper
#include <AccelStepper.h> 

// Define a stepper and the pins it will use 
AccelStepper stepper; // Defaults to 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 
#define ANALOG_IN A1 

void setup() 
{   
  stepper.setMaxSpeed(100); 
  stepper.setAcceleration(100); 

void loop() 
  // Read new position 
  static int last_analog_in; 
  int analog_in = analogRead(ANALOG_IN); 
  if (analog_in != last_analog_in) 
  { 
    stepper.moveTo(analog_in); 
    last_analog_in = analog_in; 
  } 
  stepper.run(); 

i used this

Lim Gabriel

unread,
Mar 9, 2017, 12:15:52 PM3/9/17
to accelstepper
its the same motor knob sketch but my stepper doesnt move:(


On Thursday, March 9, 2017 at 11:27:41 PM UTC+8, gregor wrote:

Mike McCauley

unread,
Mar 9, 2017, 2:44:04 PM3/9/17
to accels...@googlegroups.com, Lim Gabriel
Suggest you start with the simpler bounce sketch example, and check your motor
wiring.

After that, suggest you read:

http://www.catb.org/esr/faqs/smart-questions.html

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

James Cullins

unread,
Jan 26, 2019, 9:51:18 AM1/26/19
to accelstepper
Hello,
I tried this example and it works well but when the motor is at the commanded speed the dir pin changes very fast causing motor noise as it changes.
Is there a fix or do I just have a noisy pot?  I am using Arduino IDE with an STM32F103 using a 12 bit analog and a 10k linear pot . Thank you
Reply all
Reply to author
Forward
0 new messages