Running two stepper motors at the same time on Motor Shield V2

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
djanesko
 
Posts: 2
Joined: Tue Sep 10, 2013 2:46 pm

Running two stepper motors at the same time on Motor Shield V2

Post by djanesko »

Hello,

I am trying to run two stepper motors at the same time. I need the two motors to turn back and forth at different random intervals at the same time. The code as it is now the motor in port 1 goes back and forth first then stops then the port motor runs.

Something like -
motor 1 and motor2 two move forward at random interval 1 and random interval 2 respectively
motor 1 and motor2 two move backward at random interval 1 and random interval 2 respectively

Is there a way to write the code so that each motor is controlled independently? This would be ideal.

Thanks!

Code: Select all

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

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *myMotor1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *myMotor2 = AFMS.getStepper(200, 2);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps

  AFMS.begin();  // create with the default frequency 1.6KHz
  
  myMotor1->setSpeed(20);  //  rpm 
  myMotor2->setSpeed(20);  //  rpm     
}

void loop() {
  myMotor1->step(random(30,50), FORWARD, DOUBLE); 
  myMotor1->step(random(30,50), BACKWARD, DOUBLE);
  myMotor2->step(random(30,50), FORWARD, DOUBLE); 
  myMotor2->step(random(30,50), BACKWARD, DOUBLE);
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running two stepper motors at the same time on Motor Shield V2

Post by adafruit_support_bill »

You can use Adafruit's version of AccelStepper to move both motors at the same time. See the Accel_Xxxx examples in the V2 motor shield library.
https://github.com/adafruit/AccelStepper

User avatar
djanesko
 
Posts: 2
Joined: Tue Sep 10, 2013 2:46 pm

Re: Running two stepper motors at the same time on Motor Shield V2

Post by djanesko »

Thanks!
This gets me closer.

I got the code to kind of work. There seems to be some problem in the void loop. I added the moveto commands random command and range ( stepper2.moveTo(random(30,50))...) into void loop so I get a new random back or forth value each cycle. But the motor is only moving back and forth one or two steps.

Also here is a better description of exactly what I want the motors to do, to help in making my request clearer.
I am using the motor to turn potentiometers. Each motor will turn back and forth a random number of steps between 30 and 50 steps, with a new random step count being assigned after each step and unique to each motor. The total movement also needs to be constrained within a range of about 100 steps, this will keep the pot with a specific range of resistances. I think I can do the latter in the if statement - something like
if (stepper1.distanceToGo() == (100||200) )
stepper1.moveTo(-stepper1.currentPosition()); ?

Code: Select all

#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, 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 3 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);


void setup()
{  
  AFMSbot.begin(); // Start the bottom shield
   
  stepper1.setMaxSpeed(30.0);
  stepper1.setAcceleration(100.0);
  
  stepper2.setMaxSpeed(30.0);
  stepper2.setAcceleration(100.0);
  
}

void loop()
{
  stepper1.moveTo(random(30,50));
  stepper2.moveTo(random(30,50));
  
    //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();
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Running two stepper motors at the same time on Motor Shield V2

Post by adafruit_support_bill »

You are calling moveTo(random...) on every pass through the loop.

Code: Select all

void loop()
{
  stepper1.moveTo(random(30,50));
  stepper2.moveTo(random(30,50));
You need to track whether each motor is going forward or reverse and only generate another random moveTo() when it returns to the home position.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”