Running two motors

by garethpletts on Wed Nov 07, 2012 8:58 pm

Hi,

Im relatively new to Arduino so would greatly appreciate some code advice.

I need to run two DC Motors using the Adafruit motor shield. I need to run one motor forward on M1 for 2450, then run another motor on M2 after M1 for 2600, then stop both of them for 30000, then repeat again.

I have managed to work out how to do one motor on its own (as shown below), but I cant work out how to run two motors as mentioned.


Code: Select all
AF_DCMotor motor1 (1, 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(255);     // set the speed to 200/255
}

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

  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(30000);
}


Does anyone know how this would be done?

Many thanks :)
garethpletts
 
Posts: 1
Joined: Wed Nov 07, 2012 8:45 pm

Re: Running two motors

by adafruit_support_bill on Thu Nov 08, 2012 6:09 am

The code you posted won't work. In fact it won't even compile. If you define your motor as "motor1" you have to use "motor1" in all the commands.
If you want to run 2 motors, you have to define 2 motors:
Code: Select all
AF_DCMotor motor1 (1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2 (1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm


When you want to run motor 1 you use "motor1" commands.
When you want to run motor 2 you use "motor2" commands.
User avatar
adafruit_support_bill
 
Posts: 25524
Joined: Sat Feb 07, 2009 9:11 am