Using AccelStepper Library with Adafruit Motor Shield V2

1,752 views
Skip to first unread message

Eric Holzapfel

unread,
Oct 3, 2014, 3:24:48 PM10/3/14
to accels...@googlegroups.com
Hello Members,
I have been searching the Adafruit forum (Motor Shield V2), for methods
to use the AccelStepper lib with the Motor Shield V2 - I have seen on post
where the alternate constructor is used more or less in the code below.  Another post in the Adafruit forum, said "We do not use the AccelStepper Library much here, 
go to the AccelStepper forum"  so here I am.

Using the code below I am unable to control the direction I want to motor to move, and it steps in a direction (according to the constructor func), one step at a time.

I wanted to add some acceleration to a program that uses the motor shield exclusively.
That program moves the motor (either left or right according to which button is pushed), hits a limit switch, backups 
a little and stops.  The a left or right button can be pushed, the motor moves in the opposite direction until it hits limit switch
at the other "end".  I use a pot to vary the speed of the motor.  I was thinking I would like to add some
acceleration to the start of the motor movement, and maybe deceleration which the limit switch is hit.

Can I use the combination of the AccelStepper library and the Adafruit Motor shield this way?
I am open to suggestions and code hints.
thanks,
eholz1/ewholz

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

// AccelStepper Code
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep1() {  
  myMotor->onestep(BACKWARD, DOUBLE); 
}

void backwardstep1() {  
  myMotor->onestep(FORWARD, DOUBLE);
}

AccelStepper Astepper1(forwardstep1,backwardstep1); // Alternate constructor

void setup()
{  
   Serial.begin(9600);           // set up Serial library at 9600 bps
   Serial.println("Stepper test!");
  
   AFMS.begin();  // create with the default frequency 1.6KHz
   //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
   Astepper1.setMaxSpeed(200.0);
   Astepper1.setAcceleration(100.0);
   Astepper1.moveTo(324);
   //Astepper1.setSpeed(80);   
}

void loop()
{

Astepper1.runSpeed();

}




Mike McCauley

unread,
Oct 3, 2014, 7:44:53 PM10/3/14
to accels...@googlegroups.com
Hello,

Im not completely clear on what your software question is. I think this is it:

Using the code below I am unable to control the direction I want to motor to
move,

Do you mean that it alwyas turns the same direction regardless of the target
position?

If so, are you sure you have your stepper motor wired up correctly?

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

gregor

unread,
Oct 7, 2014, 7:17:49 AM10/7/14
to accels...@googlegroups.com
i'm afraid i do not understand the problem, but i noticed this in your code:
your forwardstep function makes the stepper motor step backwards and vice versa. did you define this on purpose?

Eric Holzapfel

unread,
Oct 7, 2014, 7:31:23 PM10/7/14
to accels...@googlegroups.com
Hello and thanks for follow-up,

I was trying to figure a way to use the Accelstepper library with the 
Adafruit motor shield.  The base code just runs the motor in the "forward"
or "backward" direction - that is of course, because the functions use the adafruit code to step the motor.

I see this is really only a compromise - it would be better to use only the AccelStepper library with a different controller,
or use only the Adafruit library and the Adafruit shield.

Thanks for the input,

ewholz

mompert...@web.de

unread,
Oct 15, 2014, 8:48:21 AM10/15/14
to accels...@googlegroups.com
Hey Eric,

just saw your post and I am having a similar setup: I use the Adafruit Motorshield 2.0 with my Arduino Uno plus the accelstepper library.

I am totally new to programming etc. and just need a very basic code that drives two stepper motors.
I am found other examples on the net but they are more sophisticated (with MoveTo functions etc.).

All I am looking for is the code that allows me to drive two stepper motors individually from each other; let's say

Motor A runs with speed X clockwise
Motor B runs with speed Y counterclockwise

And both run endlessly - meaning they do NOT stop after a specific amount of steps, time or else.

Reading your post, it seems you just achieved such code.
Would you be able to share it with me?

Best regards in advance,
Daniel

Eric Holzapfel

unread,
Oct 22, 2014, 3:26:16 PM10/22/14
to accels...@googlegroups.com
hello mompert.daniel,

did you check the library - and in there is an example of how to program the motor using
the adafruit shield - the demo is located in the example folder, called "MotorShield"  this shows the commands to use.

--
You received this message because you are subscribed to a topic in the Google Groups "accelstepper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/accelstepper/vxH3r7dn8x4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages