Arduino Uno + Adafruit Motorshield V2 + AccelStepper

579 views
Skip to first unread message

mompert...@web.de

unread,
Oct 15, 2014, 9:33:11 AM10/15/14
to accels...@googlegroups.com
Dear all,

I am new to the coding univers and thus have big issues with a small task:

All I need are my two stepper motors to be controlled independently; meaning

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

where both run endlessly.

I have found various codes on the web - however all with fancy additions (MoveTo and "if" functions).

Can anyone provide respective code?

This, for example, is a code that works with my setup
but I don't understand its structure and would need it to be stripped of all unecessarites.
If I do this, the code stops working ;-(

Thanks in advance for all help.
Daniel



Code:


#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed

// Connect two steppers with 200 steps per revolution (1.8 degree) to the top shield
Adafruit_StepperMotor *myStepper1 = AFMSbot.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMSbot.getStepper(200, 2);


// wrappers for the first motor!
void forwardstep1() {myStepper1->onestep(FORWARD, SINGLE);}
void backwardstep1() {myStepper1->onestep(BACKWARD, SINGLE);}

// wrappers for the second motor!
void forwardstep2() {myStepper2->onestep(FORWARD, DOUBLE);}
void backwardstep2() {myStepper2->onestep(BACKWARD, DOUBLE);}


// Now we'll wrap the 2 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()

  AFMSbot.begin(); // Start the bottom shield
  
  stepper1.setSpeed(800.0);  //setMaxSpeed
  stepper1.setAcceleration(25.0);
  stepper1.moveTo(10000);
 
  stepper2.setSpeed(100.0);
  stepper2.setAcceleration(125.0);
  stepper2.moveTo(10000);
}

void loop()
{
    stepper1.run();
    stepper2.run();
}

Sandy Noble

unread,
Oct 15, 2014, 5:02:53 PM10/15/14
to accels...@googlegroups.com
There are no unnecessary lines in that sketch, if you want to use Accelstepper with Adafruits Motorshield v2.

Structurally, there is a declaration portion:

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed

// Connect two steppers with 200 steps per revolution (1.8 degree) to the top shield
Adafruit_StepperMotor *myStepper1 = AFMSbot.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMSbot.getStepper(200, 2);


// wrappers for the first motor!
void forwardstep1() {myStepper1->onestep(FORWARD, SINGLE);}
void backwardstep1() {myStepper1->onestep(BACKWARD, SINGLE);}

// wrappers for the second motor!
void forwardstep2() {myStepper2->onestep(FORWARD, DOUBLE);}
void backwardstep2() {myStepper2->onestep(BACKWARD, DOUBLE);}


// Now we'll wrap the 2 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

That will need to be at the top of any code you use this in. Each line in this builds on the previous declaration. Working backwards from the end, 
- AccelStepper stepper1 is built with two functions forwardstep1, backwardstep1. It wraps and uses these two functions.
- forwardstep1 and backwardstep1 are both based on a function (.onestep(...)) from myStepper1. They wrap access to myStepper1.
- myStepper1 is created from AFMSbot.
- The lines with #include at the beginning load up pre-written blocks of code that you'll use to control. One of these, obviously is Accelstepper.

In practice, you will only actually use the very last object - stepper1 (and stepper2).

There is a procedural set up portion

void setup()
{  
  AFMSbot.begin(); // Start the bottom shield
   
  stepper1.setSpeed(800.0);  //setMaxSpeed
  stepper1.setAcceleration(25.0);
  stepper1.moveTo(10000); 
  
  stepper2.setSpeed(100.0);
  stepper2.setAcceleration(125.0);
  stepper2.moveTo(10000);
}

You can probably read this easily enough, the only cryptic line is the first one AFMSbot.begin(). Don't worry about it, but it does need to be in your code somewhere before you try to use the motorshield.

The looping bit is easy:

void loop()
{
    stepper1.run();
    stepper2.run();
}


Consider whether you need accelstepper - it's got great features, but if you don't need them then it is an extra layer of indirection that maybe you don't need.

Adafruit_MotorShield/StepperTest.ino is an example of the standard way of doing things.


sn



--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sandy Noble

mompert...@web.de

unread,
Oct 16, 2014, 10:11:20 AM10/16/14
to accels...@googlegroups.com
Hey Sandy,

thanks for your feedback on this.

I understand that the code is fine as it is. However, it features functions/commands that I do not need in my planned code.

I don't need
- any backwardstep option
- no moveTo option
- no accelaration option

Once I start to clean this from the code, it stops working.

I assume the following would be enough for me; but how do I wrap the motors?


#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"


Adafruit_MotorShield AFMSbot(0x61);

Adafruit_StepperMotor *myStepper1 = AFMSbot.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMSbot.getStepper(200, 2);


void forwardstep1() {myStepper1->onestep(FORWARD, SINGLE);}
void backwardstep1() {myStepper1->onestep(BACKWARD, SINGLE);} // What is the backwardstep for? I just need constant running in one direction, not back and forth

void forwardstep2() {myStepper2->onestep(FORWARD, DOUBLE);}
void backwardstep2() {myStepper2->onestep(BACKWARD, DOUBLE);}

AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);


void setup()
{  
  AFMSbot.begin();
   
  stepper1.setSpeed(200.0);   
  stepper2.setSpeed(100.0);
}



void loop()
{
    stepper1.run();
    stepper2.run();
}



Sandy Noble

unread,
Oct 16, 2014, 6:06:56 PM10/16/14
to accels...@googlegroups.com
Fair enough, you don't need backwards movement - just re-use your forwardstep() methods and remove the declarations for backwardstep.

AccelStepper stepper1(forwardstep1, forwardstep1);
AccelStepper stepper2(forwardstep2, forwardstep2);

The main feature of accelstepper is that it accelerates and decelerates for you. To do that, it needs to know how far you want to move. You tell it that by using move() or moveTo(). It's just how it works.

If you do not need planned movement, or acceleration, then you do not need AccelStepper, and in fact you'll have to work _around_ those built-in features to get it to do what you want. Why not just use the Adafruit stepper library directly as below?

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"


Adafruit_MotorShield AFMSbot(0x61);

Adafruit_StepperMotor *myStepper1 = AFMSbot.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMSbot.getStepper(200, 2);

void setup()
{  
  AFMSbot.begin(); 
  stepper1->setSpeed(200);    
  stepper2->setSpeed(100);
}



void loop()
{
    stepper1->step(1);
    stepper2->step(1);
}
Reply all
Reply to author
Forward
0 new messages