You are on page 1of 2

Stepper

 control  using  the  accelstepper  library  


 
Control  stepper  speed  with  a  potentiometer:  
 
#include  <Servo.h>  
#include  <AccelStepper.h>  
#include  <Wire.h>  
#include  <Adafruit_MotorShield.h>  
//#include  "utility/Adafruit_PWMServoDriver.h"  
 
Adafruit_MotorShield  AFMS  =  Adafruit_MotorShield();  
Adafruit_StepperMotor  *joepStepper  =  AFMS.getStepper(64,  1);  
 
void  forwardstep1()  {  
   joepStepper-­‐>onestep(FORWARD,  SINGLE);  
}  
void  backwardstep1()  {  
   joepStepper-­‐>onestep(BACKWARD,  SINGLE);  
}  
AccelStepper  Astepper1(forwardstep1,  backwardstep1);  
 
int  potVal  =  0;  
float  stepperSpeed  =  0.0;  
unsigned  long  lastTime1  =  0;  
int  POT  =  0;  
 
 
void  setup()  {  
   AFMS.begin();  
   Astepper1.setSpeed(100);  
}  
 
 
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  
 
}  
 
   
Control  stepper  position  with  a  potentiometer:  
 
#include  <Servo.h>  
#include  <AccelStepper.h>  
#include  <Wire.h>  
#include  <Adafruit_MotorShield.h>  
//#include  "utility/Adafruit_PWMServoDriver.h"  
 
Adafruit_MotorShield  AFMS  =  Adafruit_MotorShield();  
Adafruit_StepperMotor  *joepStepper  =  AFMS.getStepper(64,  1);  
 
void  forwardstep1()  {  
   joepStepper-­‐>onestep(FORWARD,  SINGLE);  
}  
void  backwardstep1()  {  
   joepStepper-­‐>onestep(BACKWARD,  SINGLE);  
}  
AccelStepper  Astepper1(forwardstep1,  backwardstep1);  
 
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  
 
}  
 

You might also like