Basic AccelStepper Code to control 2 stepper motors

4,733 views
Skip to first unread message

mompert...@web.de

unread,
Jun 15, 2014, 3:42:12 PM6/15/14
to accels...@googlegroups.com
Dear all,

I am having problems with the library.
I need a very basic sketch for my Arduino project that allows the following

- running two stepper motors at the same time
- with individual speeds and direction

Example:

Motor A running clowckwise with a speed of 100

while

Motor B running counterclockwise with a speed of 50


My Setup is an Arduino Uno Rev3 and Adafruit Motor Shield Version 2.0

Thanks for your help
Daniel

Sandy Noble

unread,
Jun 15, 2014, 3:59:14 PM6/15/14
to accels...@googlegroups.com
Hey Daniel, do you mean exactly like the example sketch, AFMotor_MultiStepper.ino?

Lets have a look at what you've got so far, and what problems you've had.


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,
Jun 15, 2014, 4:17:10 PM6/15/14
to accels...@googlegroups.com
Hi Sandy,

thanks so much for your response.

This is what I have so far (copied together from different sketches)
I am new to coding so I tried to read and understand the code as far as possible.
The code below however has various malfunctions as it only runs once I have loaded random other sketches into the Arduino.
If I start this code as the first one after starting the Arduino software --> nothing happens.



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

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

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

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!

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

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


void setup()
{
  AFMSbot.begin(); // Start the bottom shield
  
  stepper1.setMaxSpeed(180.0);      // Geschwindgkeit Motor 1
  stepper1.setAcceleration(80.0);
 
  stepper2.setMaxSpeed(30.0);      // Geschwindgkeit Motor 2
  stepper2.setAcceleration(100.0);
 
}

void loop()
{
  stepper1.moveTo(random(20000,100));    // Umdrehungen Motor 1
  stepper2.moveTo(random(20000,200));    // Umdrehungen Motor 2
 
    //Change direction at the limits
    if (stepper1.distanceToGo() == 0 )
   stepper1.moveTo(-stepper1.currentPosition());

    if (stepper2.distanceToGo() == 0 )
   stepper2.moveTo(-stepper2.currentPosition());

    stepper1.run();
    stepper2.run();
}

Sandy Noble

unread,
Jun 15, 2014, 4:51:14 PM6/15/14
to accels...@googlegroups.com
Only thing I can see wrong with this is the position of the .moveTo(random(20000, 100)) command. This actually changes the target every single time it runs, and one of the side-effects of calling .moveTo(..) is that it sets the speed to 0. So I think this sketch means the motor can never get started, because it's speed and direction is constantly being re-set.

If you move those two lines into the setup() section, you might have better luck. I think it probably _was_ working for you, it just rarely resulted in any visible movement. 

Also the random command takes parameters in the opposite order - http://arduino.cc/en/Reference/Random says random(min, max). Not quite sure what I'd expect it's behaviour to be when the min is larger than the max. So maybe stepper1.moveTo(random(100,20000)) is better.

What this sketch would do then is:

1. In setup(), set a speed and acceleration for each motor, 
2. In setup(), pick a random target for each motor, between 100 (or 200) and 20,000.
3. In loop(), call .run() on each motor. This will move a motor a maximum of one step towards the target.
4. In loop(), check if each motor has reached it's target yet, and if it has, set it's target to be the inverse of it's current position. So if it got to 15,000, it will now be sent to head back to -15,000.

If that's what you want to happen.

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,
Jun 16, 2014, 8:40:18 AM6/16/14
to accels...@googlegroups.com
Sandy,

thanks again - this is understandable and hopefully helpful, too.
I will check it tonight.

However, what I don't understand are these parts in general:

A)
As far as I understand this, it tells the morots to go one stepforward and one step backward
What is the sense behind this?


// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!

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

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


and


B)
Why would I need the moveTo Function if I only need constand running (for now)?


  //Change direction at the limits
    if (stepper1.distanceToGo() == 0 )
   stepper1.moveTo(-stepper1.currentPosition());

    if (stepper2.distanceToGo() == 0 )
   stepper2.moveTo(-stepper2.currentPosition());


Best regards,
Daniel


Sandy Noble

unread,
Jun 16, 2014, 9:48:16 AM6/16/14
to accels...@googlegroups.com
Hi, 

A) Almost right, what you're doing there with the forwardstep1(), backwardstep1() etc is creating a function that can be called to make the motor move one step back or forth. But at this point, you aren't actually calling those functions to make the motor move, instead you're passing the functions to the AccelStepper object when you create it:

AccelStepper stepper1(forwardstep1, backwardstep1);

So this says to create an accelstepper object, but this time the accelstepper library doesn't need to worry about wiring connections, setting things high or low, because those things are taken care of by the Adafruit library. What you pass in to accelstepper is simply those two functions, one for moving forwards, one for moving backwards, and accelstepper can call these functions whenever it wants to move the motor. It doesn't need to know (or care) exactly how these work. It's like accelstepper is given a backwards lever and a forwards lever, and doesn't care what they're attached to, it just needs something to pull.

The reason they are only one step is because accelstepper only ever moves one step at a time, so that's all it needs.

It looks a bit weird when you see it written down in code, but it's actually a very neat way of providing a consistent interface, and is not uncommon.

B) I was suggesting you move the .moveTo() commands where you set the initial targets. This is to prevent them from continually changing the target position and re-setting the motor speed.

The other .moveTo() commands (stepper1.moveTo(-stepper1.currentPosition());) are only run when the motor has reached it's target and stopped, and they legitimately set a new target, so that the motor keeps moving. I've made the little change below:

void setup()

  AFMSbot.begin(); // Start the bottom shield
  stepper1.setMaxSpeed(180.0);      // Geschwindgkeit Motor 1
  stepper1.setAcceleration(80.0);
  stepper2.setMaxSpeed(30.0);      // Geschwindgkeit Motor 2
  stepper2.setAcceleration(100.0);

  stepper1.moveTo(random(20000,100));    // Umdrehungen Motor 1
  stepper2.moveTo(random(20000,200));    // Umdrehungen Motor 2
}

void loop()
{

 
    //Change direction at the limits
    if (stepper1.distanceToGo() == 0 ) {
        stepper1.moveTo(-stepper1.currentPosition());
    }

    if (stepper2.distanceToGo() == 0 ) {
        stepper2.moveTo(-stepper2.currentPosition());
    }

    stepper1.run();
    stepper2.run();
}

mompert...@web.de

unread,
Jun 19, 2014, 3:34:16 PM6/19/14
to accels...@googlegroups.com

hey sandy,

i found some time to check things out which we discussed here.
this is the code i tried (according to your suggestions from above).
i also changed the values around in line
stepper1.moveTo(random(20000,100))
both cases didnt work.
 
It is getting frustrating as it seems i am too stupid :-(


If it is ok for you, i ll list some further questions and anotations in the code for further understanding.



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

(Do I need to include all of these? I have an Arduino Uno Rev and the Adafruit Motor Shield Version 2 stacked upon it.)



Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

(I dont understand these two lines. I guess this somehow activates the Motor Shield but I have no clue about the Pins. One of my motors is connected to the block which indicates M1 and M2 and the other one is connected to M3 and M4.)



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

(This initilizes the motors and defines them by the names *mystepper1  and *mystepper2 ?!)


// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!

(still no clue what a "wrapper" is and does. What do i need it for?)



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

// Now we'll wrap the 2 steppers in an AccelStepper object


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


(What’s the object for?)

void setup()

  AFMSbot.begin(); // Start the bottom shield
  stepper1.setMaxSpeed(180.0);      // Geschwindgkeit Motor 1
  stepper1.setAcceleration(80.0);
  stepper2.setMaxSpeed(30.0);      // Geschwindgkeit Motor 2
  stepper2.setAcceleration(100.0);

  stepper1.moveTo(random(20000,100));    // Umdrehungen Motor 1
  stepper2.moveTo(random(20000,200));    // Umdrehungen Motor 2
}

void loop()


{
 
    //Change direction at the limits
    if (stepper1.distanceToGo() == 0 ) {
        stepper1.moveTo(-stepper1.currentPosition());

    }

    if (stepper2.distanceToGo() == 0 ) {
        stepper2.moveTo(-stepper2.currentPosition());

    }

    stepper1.run();
    stepper2.run();

}

I think all I need would be something like


- Include the respective libraries

- Define/initialize motor shield

- Define motors

- void setup

                Function to make both motors run with specific direction (counter/clockwise) and speed

 

So sorry, Sandy, for my incompetence.

Sandy Noble

unread,
Jun 19, 2014, 5:56:27 PM6/19/14
to accels...@googlegroups.com
Ignorance is nothing to be ashamed of, but yes it can be frustrating.

You're right about the steps in the sketch, but there's a step between define motors and setup - I'll add it in below.

Step1. Include the respective libraries.

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

The above are all necessary.

Step 2. Define and initialize motor shield.

Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed 
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

 These two statements actually describe an Arduino with TWO motorshields stacked on top of them. The first one is called AFMSbot (presumably the one on the bottom), and it is connected on a particular, unusual address (0x61).  The second shield is called AFMStop (I guess, on the top), and is set up to use address 0x60. So normally, you'd specify which pins the motor controller uses, but in this case you're specifying addresses, that's because these motorshields are all connected on the same communications buss - they actually share pins, but they each have a special address on that buss.

So, you only have one motorshield connected, so in your case, only one of these shield objects is actually connected to anything physical, and it is the second one, AFMStop. It is the second one because of the address: 0x60 is the default address that all motorshields have. You need to do a bit of modification on the board itself to set the address to anything else, so I'll assume you haven't done that, and you are using the default address.

You can take the top line (AFMSbot) out entirely, it doesn't do anything.

Step 3. Initialize the motors.

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

Each line defines a motor indeed, giving it a name like myStepper1. The number in the last part (AFMStop.getStepper(200, 1)) controls which side of the motorshield that motor is on. So 1 is on the left (M1 and M2?) and 2 is on the right (M3 and M4).

(The * asterisk on the beginning of the variable name is NOT actually part of the name, but instead indicates that this variable is a pointer to an object, rather than the object itself. Using pointers adds a whole level of indirection and confusion to the mix, so I recommend you don't worry about them at all just accept that here, when the motor variable is declared, it needs a star on the front of it.)


Step 4. Set up Accelstepper.

The adafruit library allows you to send commands to the motorshield, and have it understand them. You don't need accelstepper at all. If you want to move motors simultaneously, or with smooth acceleration, then it is awesome.

Because the adafruit shield can only be controlled using the adafruit library, then Accelstepper must be shown how to use the adafruit library. That's what those four  small functions below do:

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

They encapsulate everything that is required to be able to move each motor in either direction. Now you have this set of four functions, you can call any one of them, at any time, and it'll move a motor, and you don't have to care about how it actually does it. It wraps the unique bit of code (myStepper1->onestep(FORWARD, DOUBLE)) with a bland, reusable function. (This is an extremely common method that you'll see in code of any appreciale size: layers and layers of abstraction, each one wrapping up the complexity of the previous layer with a smooth interface.)

Then each pair of functions are further wrapped by an AccelStepper object:

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

The AccelStepper object is a beautiful bundle of code object that knows how to do accelerations, track the position of the motor, and tell whether a step is required in order to keep up a certain speed. It has functions attached to it for setting target (moveTo()), running (run()) etc.


(What’s the object for?)
The object is essentially your interface into the AccelStepper library.  

So AccelStepper wraps around Adafruit_MotorShield, so that you don't need to use it directly - it uses the motorshield on your behalf. (In turn, Adafruit_Motorshield wraps around Wire.h, which is why Wire.h is imported at the top of the sketch.)

Step 5. setup(), in which we start talking to the motorshield, and tell the motor objects how fast you want them to go. Finally, you set the initial target position for each motor.

void setup()

  AFMSbot.begin(); // Start the bottom shield

  stepper1.setMaxSpeed(180.0);      // Geschwindgkeit Motor 1

  stepper1.setAcceleration(80.0);
  stepper2.setMaxSpeed(30.0);      // Geschwindgkeit Motor 2
  stepper2.setAcceleration(100.0);

  stepper1.moveTo(random(20000,100));    // Umdrehungen Motor 1
  stepper2.moveTo(random(20000,200));    // Umdrehungen Motor 2
}


You've still got the parameters to the random() function the wrong way around. As specified in the docs (http://arduino.cc/en/Reference/Random), the syntax is:

    random(min, max)

So you want random(200, 20000) to give you a number between 200 and 20,000.

This compiles ok for me, but I don't have a motorshield to test it on right here.


sn

mompert...@web.de

unread,
Jun 19, 2014, 6:27:08 PM6/19/14
to accels...@googlegroups.com
hey sandy,

thanks so much!

I compiled it just like you described it and fixed the min/max issue, too.

I get the follwoing error message upon compiling:

core.a(main.cpp.o): In function `main':
C:\Arduino\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop'


with this code


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

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

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


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


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

void setup()
{
  AFMSbot.begin(); // Start the bottom shield

  stepper1.setMaxSpeed(180.0);      // Geschwindgkeit Motor 1

  stepper1.setAcceleration(80.0);
  stepper2.setMaxSpeed(30.0);      // Geschwindgkeit Motor 2
  stepper2.setAcceleration(100.0);

  stepper1.moveTo(random(100,500));    // Umdrehungen Motor 1
  stepper2.moveTo(random(100,400));    // Umdrehungen Motor 2
}


greets & thanks,
Daniel

Sandy Noble

unread,
Jun 19, 2014, 6:40:45 PM6/19/14
to accels...@googlegroups.com
Ah right sorry you need the loop() function too:

void loop()


{
 
    //Change direction at the limits
    if (stepper1.distanceToGo() == 0 ) {
        stepper1.moveTo(-stepper1.currentPosition());

    }

    if (stepper2.distanceToGo() == 0 ) {
        stepper2.moveTo(-stepper2.currentPosition());

    }

    stepper1.run();
    stepper2.run();

}


The other thing is that you have got the AFMS address set wrong:

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

The above specified a motorshield at address 0x61. Unless you have modified your shield to be at that address (you'd know if you had), then it'll be on the default address which is 0x60. If you don't specify an address, 0x60 gets used, so it's easier just not to mention it:

Adafruit_MotorShield AFMSbot(); // Use default address


good luck :)

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

Sandy Noble

unread,
Jun 19, 2014, 6:41:55 PM6/19/14
to accels...@googlegroups.com
Lets try that again, the line should be

Adafruit_MotorShield AFMSbot = Adafruit_MotorShield(); 

sn

mompert...@web.de

unread,
Jun 19, 2014, 7:01:40 PM6/19/14
to accels...@googlegroups.com
GREAT!!!!!

Guess what? It works!!!


However, I have to deep dive into the code again as one motor now makes about two rotations, then slows down and then speeds up again but in the counter direction and it starts all over again.

This is a great function I will definitely use in the future but in the first place, both steppers should run continously.
I guess this movement now derives from the .moveTo function?

Thanks so so much. I deeply appreciate your help and patience!!!
Best, Daniel

Sandy Noble

unread,
Jun 20, 2014, 8:30:13 AM6/20/14
to accels...@googlegroups.com
That's good :) Right, but both motors should be moving a random distance in one direction (that's the random number you generated in the setup()), then when they each reach the end of their distances, they will stop and reverse, and go TWICE that distance in reverse. Then stop, and do it all again. That's what your code does, and was always designed to do.

Do you just want both motors turning endlessly? At different speeds?

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,
Jun 20, 2014, 11:02:46 AM6/20/14
to accels...@googlegroups.com

hey sandy,

yeah, exactly.

The code we worked on the entire time was a code i found online that somehow got my motors running. I did not get anything to work with the accel stepper library myself.

Like I said in the first post

I need the accel stepper library just to do the following

-          Have both motors running endlessly with different speeds and different directions

So that I can have for example

                Motor 1 running with a speed of 80 counterclockwise
                Motor 2 running with a speed of 150 clockwise



Daniel

Sandy Noble

unread,
Jun 21, 2014, 7:18:42 AM6/21/14
to accels...@googlegroups.com
You can do this with

void setup() {
  motorA.maxSpeed(80);
  motorB.maxSpeed(150);
  motorA.move(-2147483647L); //  negative number to move anticlockwise
  motorB.move(2147483647L); // positive number to move clockwise
}

void loop() {
  motorA.run();
  motorB.run();
}

This will not, of course, work _forever_, eventually it will reach it's target. If you move at a thousand steps a second, it'll take about a month.

Another approach is to manually set the speed, and call runSpeed():

void setup() {
  motorA.setSpeed(80);   // forward speed
  motorB.setSpeed(-150);  // reverse speed
}

void loop() {
  motorA.runSpeed();
  motorB.runSpeed();
}

This bypasses the acceleration and target checking entirely, but also doesn't do any acceleration.




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,
Jun 23, 2014, 10:00:09 AM6/23/14
to accels...@googlegroups.com
Hey Sandy,

would I then just change the code to this:
Blue is the code we finalized before, red is the new one you provided. When I changed the stepper motor names from motorA and motorB to myStepper1 and myStepper2 the compiling check said:

"Final.ino: In function 'void setup()':
Final:32: error: request for member 'setSpeed' in 'myStepper1', which is of non-class type 'Adafruit_StepperMotor*'
Final:33: error: request for member 'setSpeed' in 'myStepper2', which is of non-class type 'Adafruit_StepperMotor*'
Final.ino: In function 'void loop()':
Final:39: error: request for member 'runSpeed' in 'myStepper1', which is of non-class type 'Adafruit_StepperMotor*'
Final:40: error: request for member 'runSpeed' in 'myStepper2', which is of non-class type 'Adafruit_StepperMotor*'
"

Best,
Peter




#include <AccelStepper.h>

#include <Wire.h>

#include <Adafruit_MotorShield.h>

#include "utility/Adafruit_PWMServoDriver.h"

 

 

Adafruit_MotorShield AFMStop(0x60); // Rightmost jumper closed

 

 

Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200,1);

Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200,2);

 

 

void forwardstep1() {

  myStepper1->onestep(FORWARD, DOUBLE);

}

void backwardstep1() {

  myStepper1->onestep(BACKWARD, DOUBLE);

}

// wrappers for the second motor!

void forwardstep2() {

  myStepper2->onestep(BACKWARD, DOUBLE);

}

void backwardstep2() {

  myStepper2->onestep(FORWARD, DOUBLE); //

}

 

 

AccelStepper stepper1(forwardstep1, backwardstep1);

AccelStepper stepper2(forwardstep2, backwardstep2);

 

 

void setup()

Sandy Noble

unread,
Jun 23, 2014, 4:00:27 PM6/23/14
to accels...@googlegroups.com
setSpeed, runSpeed etc are all functions of AccelStepper, NOT functions of Adafruit_StepperMotor.  So:

void setup()

{

  stepper1.setSpeed(80);   // forward speed

  stepper2.setSpeed(-150);  // reverse speed

}

 

 

void loop()

{

  stepper1.runSpeed();

  stepper2.runSpeed();

}



Igor Fermevc

unread,
Sep 22, 2015, 4:11:45 PM9/22/15
to accelstepper


Hello!
Sorry if I'm in a wrong place, just found this discoution and it deals with similar problem I'm facing.
Whole setup is wery similar (Arduino Uno + Adafruit Motor Shield v2.3)
If Sandy can see this and would be willing to help a little I would realy apreciate it!

I have two stepper motors, and two sensors and I wanted to write a separate method for "homing" both stepers.
However, I can't figure out how to properly define or declare this method and finaly call it during setup().
I've written two while loops in setup() as follows:

...
while (digitalRead(sensor1) == LOW)
  {
    myMotor1->step(1, FORWARD, SINGLE);
  }
  myMotor1->release();
  Serial.println("Motor1 at home");
  
  while (digitalRead(sensor2) == LOW)
  {
    myMotor2->step(1, FORWARD, SINGLE);
  }
  myMotor2->release();
  Serial.println("Motor2 at home");
...
This compile OK and I'll test it tomorrow (left the HW at work :( )
What I would like is to have separate method "void goHome(motor, sensor)".
This way, I could have only one while loop, just need to call the method twice with proper arguments for each motor:

goHome(myMotorX,sensorX)
{
     while (digitalRead(sensorX) == LOW)
     {
         myMotorX->step(1, FORWARD, SINGLE);
     }
    myMotorX->release();
    Serial.println("MotorX at home");
}

This homing method will be much more than this, some precise positioning sequence, but for now it is good for initial testing.
In hope that someone will see this post, BR, Igor

Jack J

unread,
Nov 17, 2016, 4:24:39 PM11/17/16
to accelstepper
Sandy,  Your description was a fabulous and enormously helpful to me, two years after you posted it.  It was not at all clear to me from the AccelStepper docs or examples how to tell AS what shield to use.  I had no idea how to connect the Adafruit Stepper Library to AS.  You clear and detailed explanation saved me days of effort, if not more.  I just wanted to say thank you.  -  Jack
Reply all
Reply to author
Forward
0 new messages