Code for dafruit_Motor_Shield

I am trying to make an "OSEPP Motor and Servo Shield" and a Mega2560 drive 4 DC motors using Adafruit Motor Shield Library 'V1' (as that is what the shield calls for). I have the shield stacked and I can drive any ONE of the FOUR motors using the appropriate output (M1-M4), BUT I can not get more than ONE to work at a time.

The example below is based on the code in Adafruit_Motor_Shield_library\examples\MotorTest.ino, but with two motors. When run it Motor 1 will go Forward and Back but Motor 2 does nothing. What I am missing ?

#include <AFMotor.h>

 AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
 AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

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

void loop() {

  Serial.println("Start Motor 1");
  motor1.run(FORWARD);      // turn it on forward
  delay(2000);
  motor1.run(BACKWARD);     // go the other way
  delay(2000);

  Serial.println("Start Motor 2");
  motor2.run(FORWARD);      // turn it on forward
  delay(2000);
  motor2.run(BACKWARD);     // go the other way
  delay(2000);

}

Thanks
Jack

So does anyone know where I can find a detailed description of the AFMotor library ? Turns out that if I leave MOTOR12_64KHZ out of the AF_DCMotor instance definition that things work fine. A description of the parameters would be very helpful, but the library itself has nothing, not does GitHub. Tx

ps I am going to mark this solved.

Change the above to the below and see if the failing motor works...

 AF_DCMotor motor1(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
 AF_DCMotor motor2(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm

xfpd
Had tried that and it didn't help. But removing the 2nd parameter in the instance declaration fix it.

Hmm. Have you read through the library on git for an explanation?

Yes, multiple times, and I can not find any place where it explains the details of the parameters for the library calls. I also just found out that unless you define motor.setSpeed for each motor it will only drive one motor. And it is possible that has been the issue all along, as the single motor examples don't define it. Good thing this is such a good forum in terms of people helping. Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.