Pages: [1]   Go Down
Author Topic: Running two DC motors with motor shield  (Read 1739 times)
0 Members and 1 Guest are viewing this topic.
Jerseyville IL,
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need help getting this example code to run motors 3 and 4. I have read all over the page and just do not understand how to get it to run 2 motors forward.

Please help.

#include <AFMotor.h>

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

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor.setSpeed(200);     // set the speed to 200/255
}

void loop() {
  Serial.print("tick");
  
  motor.run(FORWARD);      // turn it on going forward
  delay(1000);

  Serial.print("tock");
  motor.run(BACKWARD);     // the other way
  delay(1000);
  
  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(1000);
}
Logged

Tyler LaVite
tlavite@gmail.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25770
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
AF_DCMotor motor(2, MOTOR12_64KHZ);

I'm sorry, I don't know this library, but I'm guessing that that "2" says you're not using either motor 3 or 4.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

California
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I have experience using this library.  Each motor you want to control needs to have it's own "AF_DCMotor" line to initialize it, and then each motor will need to be controlled individually.  Like so:
Code:
// you need this so you can use the adafruit motor shield library:
#include <AFMotor.h>

// this created our first motor (called "motor") on port 3 of the motorshield:
AF_DCMotor motor(3, MOTOR12_64KHZ);  

// we do it again, this time our second motor will be called "motor2" on port
// 4 of the motorshield:
AF_DCMotor motor2(4, MOTOR12_64KHZ);
// so now we have 2 motors called "motor" and "motor2"

void setup() {
 Serial.begin(9600);           // set up Serial library at 9600 bps
 Serial.println("Motor test!");
 
 // set the speed to 200 of 255 of "motor".  Note that 0 is stop,
 // 255 is full speed:
 motor.setSpeed(200);    

 // set the speed to 200 of 255 of "motor2":
 motor2.setspeed(200);
}

void loop() {
 Serial.print("tick");
 
 motor.run(FORWARD);      // turn it on going forward
 motor2.run(FORWARD);  // motor 2 goes forward as well
 delay(1000);

 Serial.print("tock");
 motor.run(BACKWARD);     // the other way
 motor2.run(BACKWARD);  //again for motor 2
 delay(1000);
 
 Serial.print("tack");
 motor.run(RELEASE);      // stopped
 motor2.run(RELEASE);  // command motor 2 to stop
 delay(1000);
}

You can run 4 motors, but you need to give them all different names as well as put them on different ports of the motor shield.  Just define them like I did in the above examples.  Also there is no reason to name your motors "motor" and "motor2" - they can be anything you want, such as "leftWheel" and "rightWheel" then you can issue commands like this:

//initialize the wheel on port 1, calling it leftWheel:
AF_DCMotor leftWheel(1, MOTOR12_64KHZ);

//then in the loop:
leftWheel.run(FORWARD);

I hope that clears it up a bit.  
-teedeeus
Logged

Jerseyville IL,
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the info i definitely understand the library allot more!

here is a video of my bot


Logged

Tyler LaVite
tlavite@gmail.com

California
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I remember seeing before the tracks and wheels you are using for your robot but I don't remember where I've seen it - can you tell me where you got the tracks?

-teedeeus
Logged

Jerseyville IL,
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

www.sparkfun.com

direct link to tracks and gear box

Tank tracks
http://www.sparkfun.com/commerce/product_info.php?products_id=321

Dual Motor gear box (crappy motors included get replacements from www.pololu.com

http://www.sparkfun.com/commerce/product_info.php?products_id=319
Logged

Tyler LaVite
tlavite@gmail.com

California
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

that's right - thank you.
Logged

Jerseyville IL,
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No problem smiley
Logged

Tyler LaVite
tlavite@gmail.com

Pages: [1]   Go Up
Jump to: