Code for controlling multiple DC motors using Adafruit motor shield

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
vulrath
 
Posts: 2
Joined: Sat Jan 22, 2011 3:03 am

Code for controlling multiple DC motors using Adafruit motor shield

Post by vulrath »

Okay, my apologies if this has already been addressed, but I have been combing through the examples looking for the answer to my little conundrum here.

I have been writing code to run on a 4-wheeled arduino-based bot controlled via a joystick (this is the same joystick I'm using - just 5 buttons and a stick, basically) connected through a really long cable.

My design makes use of two regular DC motors running in tandem (one on the left side, and one on the right), and since the wheels are fixed, I want to make it turn by reversing one of the motors at the appropriate times (which one depends on which way I want to go). The code I have written would do this, but it won't compile (probably because I don't know the proper syntax for controlling multiple motors). What is the proper syntax?

Here's what I have:
#include <AFMotor.h>

AF_DCMotor(1);
AF_DCMotor(2);

void setup()
{
  Serial.begin(9600);
  motor1.setSpeed(200);
  Serial.println("Motor 1 initialized");
  motor2.setSpeed(200);
  Serial.println("Motor 2 initialized");
}
void loop()
{
  int forward=digitalRead(3),back=digitalRead(5),left=digitalRead(7),right=digitalRead(9);
  if(forward==LOW)
  {
    Serial.println("Moving forward...");
    motor1.run(FORWARD);
    motor2.run(FORWARD);
  }
  else
  {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    Serial.println("Stopping...");
  }
  if(back==LOW)
  {
    Serial.println("Moving backward..."
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    //Serial.println("
  }
  else
  {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    Serial.println("Stopping...");
  }
  if(left==LOW)
  {
    motor1.run(FORWARD);
    motor2.run(BACKWARD);
    Serial.println("Turning left...");
  }
  else
  {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    Serial.println("Stopping...");
  }
  if(right==LOW)
  {
    motor1.run(BACKWARD);
    motor2.run(FORWARD);
    Serial.println("Turning right...");
  }
  else
  {
    motor1.run(RELEASE);
    motor2.run(RELEASE);
    Serial.println("Stopping...");
  }
}
Can you tell me how to do this?

vulrath
 
Posts: 2
Joined: Sat Jan 22, 2011 3:03 am

Re: Code for controlling multiple DC motors using Adafruit motor shield

Post by vulrath »

Never mind; I'm a moron. I forgot to define motor1 and motor2 by name when I tried to initialize them by calling:
AF_DCMotor(1);
AF_DCMotor(2);
That should have been:
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”