Drive Multiple Motors Simultaneously with the Adafruit Motor

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
Preston0258
 
Posts: 17
Joined: Fri Jul 10, 2015 3:09 pm

Drive Multiple Motors Simultaneously with the Adafruit Motor

Post by Preston0258 »

Hello,

I was wondering if there was anyway to drive multiple motors simultaneously with the Adafruit Motor Shield v2(https://www.adafruit.com/products/1438). Specifically, I am trying to drive 2 Servos, 1 DC Motor, and one Uni-polar Stepper simultaneously, but no matter what I try I can't seem to get all the motors to run at once. Any response is much appreciated. The code I am currently using is below, it does all of the functions one right after another:

Code: Select all

#include <Wire.h>
#include <Servo.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h";
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor*dcMotor = AFMS.getMotor(2);
Adafruit_StepperMotor*myMotor = AFMS.getStepper(200, 2);
Servo servo1;
Servo servo2;
int i;
int x;

void setup() {
AFMS.begin();
myMotor->setSpeed(30);
dcMotor->setSpeed(150);
servo1.attach(10);
servo2.attach(9);
}

void loop() {
myMotor->step(400, FORWARD, DOUBLE);
myMotor->step(400, BACKWARD, DOUBLE);
dcMotor->run(FORWARD);
delay(5000);
dcMotor->run(RELEASE);
delay(1000);
dcMotor->run(BACKWARD);
delay(5000);
dcMotor->run(RELEASE);
delay(1000);
for (i = 0, x = 180; i <= 180, x >= 0; i++, x--) {
servo1.write(i);
servo2.write(x); 
delay(10);
}
for (i = 180, x = 0; i >= 0, x <= 180; i--, x++) {
servo1.write(i);
servo2.write(x);
delay(10);
}
}

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

Re: Drive Multiple Motors Simultaneously with the Adafruit M

Post by adafruit_support_bill »

Stepper motor commands are 'blocking' commands. They will not return until all the steps complete.
DC motor commands are not blocking. If you tell a motor to run, it will continue running until you tell it to do something else.
Servo commands are also not blocking. But in your program you are sending them a bunch of commands in a loop, so the loop is blocking.

There are many ways to go about getting your motors to run at the same time. If you can explain a bit more what you are trying to achieve, we may be able to suggest a good approach for your project.

User avatar
Preston0258
 
Posts: 17
Joined: Fri Jul 10, 2015 3:09 pm

Re: Drive Multiple Motors Simultaneously with the Adafruit M

Post by Preston0258 »

Thanks for your input!

All that I am trying to do learn how to run these all at the same time for no specific reason other than just knowing how so I can use it in the future. Thanks again for the input.

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

Re: Drive Multiple Motors Simultaneously with the Adafruit M

Post by adafruit_support_bill »

If you re-arrange your loop like this, the DC motor and stepper will run at the same time.

Code: Select all

    void loop() 
  {
    dcMotor->run(FORWARD); // start motor running forwward
    myMotor->step(400, FORWARD, DOUBLE); // do some foreward steps
    dcMotor->run(RELEASE); // stop and rest for a bit
    delay(1000);
    dcMotor->run(BACKWARD); // start motor running backwards
    myMotor->step(400, BACKWARD, DOUBLE); // start stepping backwards
    dcMotor->run(RELEASE); // stop
    }
For some general strategies for making multiple things happen at once, see this series of tutorials:
https://learn.adafruit.com/multi-taskin ... ino-part-1
https://learn.adafruit.com/multi-taskin ... ino-part-2
https://learn.adafruit.com/multi-taskin ... ino-part-3

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

Return to “Arduino Shields from Adafruit”