Stepper Control Using The Accelstepper Library Control Stepper Speed With A Potentiometer
This document describes how to control a stepper motor's speed and position using a potentiometer with an Arduino and the AccelStepper library. It initializes the motor shield and stepper motor, then sets the stepper's speed or target position based on the potentiometer's reading in the loop function, running the stepper motor accordingly.
Stepper Control Using The Accelstepper Library Control Stepper Speed With A Potentiometer
This document describes how to control a stepper motor's speed and position using a potentiometer with an Arduino and the AccelStepper library. It initializes the motor shield and stepper motor, then sets the stepper's speed or target position based on the potentiometer's reading in the loop function, running the stepper motor accordingly.
void
loop()
{
potVal
=
analogRead(POT);
//read
the
value
of
the
potentiometer
//do
some
calculations
to
remap
the
reading
from
the
pot
meter,
speed
now
ranges
//from
0.01-‐500.0
(a
0.0
value
makes
the
stepper
turn
backwards
for
//some
reason)
(map
function
does
interger
math).
stepperSpeed
=
500*(float(potVal)/1024.0)+0.01;
Astepper1.setSpeed(stepperSpeed);
//stepperspeed
is
controlled
by
the
potentiometer
Astepper1.runSpeed();
//
give
the
stepper
a
push
to
make
sure
that
it
runs
on
the
speed
that
was
specified
int
potVal
=
0;
int
stepperPos
=
0;
unsigned
long
lastTime1
=
0;
int
POT
=
0;
void
setup()
{
AFMS.begin();
//
create
a
maximum
speed
and
an
acceleration,
these
will
be
used
to
make
the
stepper
move
//
don't
create
too
high
a
speed
as
it
will
'loose'
steps
and
no
longer
return
to
the
original
position
//
a
high
acceleration
setting
creates
a
more
'linear'
motion,
a
low
setting
an
'easing'
motion
Astepper1.setMaxSpeed(200.0);
Astepper1.setAcceleration(1000.0);
}
void
loop()
{
potVal
=
analogRead(POT);
//read
the
value
of
the
potentiometer
//put
a
target
position
in
the
stepper
(the
position
it
is
in
when
it
powers
up
//is
the
zero
position
by
default,
there
is
no
absolute
known
position
not
a
way
//to
read
it
from
the
stepper
motor.)
Astepper1.moveTo(potVal);
Astepper1.run();
//
give
the
stepper
a
push
to
make
sure
that
it
runs
on
the
speed
that
was
specified