Running two motors

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.

#include <AFMotor.h>

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 may be done?

Many Thanks :slight_smile:

garethpletts:
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.

Sure.

#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
  Serial.println("Motor test!");
  
  motor1.setSpeed(255);     // set the speed to 200/255
  motor2.setSpeed(255);     // set the speed to 200/255
}

void loop() {
  Serial.print("tick");
  motor1.run(FORWARD);      // turn it on going forward
  delay(2450);
  motor1.run(RELEASE);
  Serial.print("tock");
  motor2.run(FORWARD);
  delay(2600);
  motor2.run(RELEASE);
  Serial.print("tack");
  delay(30000);
}

Note that the serial prints will add some small delay between the motor runs.

Ah thats perfect! Does exactly what I want it to do... thanks very much :) :)