Go Down

Topic: Running two DC motors with motor shield (Read 2 times) previous topic - next topic

tlavite

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);
}
Tyler LaVite
tlavite@gmail.com

AWOL

Code: [Select]
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.
"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.

teedeeus

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: [Select]

// 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

tlavite

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

here is a video of my bot

http://www.youtube.com/watch?v=ZgqZ1_MRXZE
Tyler LaVite
tlavite@gmail.com

teedeeus

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

tlavite

Tyler LaVite
tlavite@gmail.com

teedeeus


tlavite

Tyler LaVite
tlavite@gmail.com

Go Up