0
\$\begingroup\$

I am creating a simple 4 wheel robot (BO Motors 200 RPM) with Adafruit Motor Shield and Arduino Uno V3. I am trying to use a Bluetooth module to control the robot. However, unlike Mega, I do not have TX1 and RX1 pins on Uno, only 1 pair of TX and RX pins. So, it's a bit difficult to connect the bluetooth module because I realize that it needs soldering, jumper connection won't be possible.

I have following 3 questions. Any help is appreciated.

  1. Which Arduino pins are actually used by the Adafruit Motor Shield? If I want to do Serial IO through SoftwareSerial, which Digital pins are safe (free/unused) to use?
  2. If I continue to use the Arduino's Tx/Rx pins instead of using SoftwareSerial via Digital ports, will that be a problem? Are those pins available for use when the shield sits on top of Arduino?
  3. Following is my code. When the robot runs, I only get one rear wheel running for almost any Serial input. Rest of the motors doesn't run. Will you please take a look at the code and see if anything looks wrong here.
#include <AFMotor.h>
char incomingByte;
int LED = 11;
#define MAX_DISTANCE 2000
#define MAX_SPEED 240
#define COLL_DIST 25
#define TURN_DIST COLL_DIST + 10
// Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_8KHZ);
AF_DCMotor motor4(4, MOTOR12_8KHZ);
int pos = 0;
int maxDist = 0;
int maxAngle = 0;
int maxRight = 0;
int maxLeft = 0;
int maxFront = 0;
int course = 0;
int curDist = 0;
String motorSet = "";
unsigned long timeNow = 0;
int maxOffsetSpeed = 20;
int right = 0, left = 0, front = 0, back = 0;
int issettings = false;
void setup() {
  Serial.begin(9600); // initialization
}

void loop() {
  if (Serial.available() > 0) { // if the data came
    timeNow = millis();
    incomingByte = Serial.read();
    if (issettings == true) {
      maxOffsetSpeed = incomingByte;
      return;
    }
    if (incomingByte == 's') {
      issettings = !issettings;
      return;
    }
    if (incomingByte == '1') {
      // front
      if (front < MAX_SPEED && back == 0) {
        back = 0;
        front = front + 40;
        moveForward(front);
      } else if (back > 0) {
        front = 0;
        back = back - 40;
        moveBackward(back);
      }
    }
    if (incomingByte == '2') {
      // back
      if (front > 0 && back == 0) {
        back = 0;
        front = front - 40;
        moveForward(front);
      } else if (back < MAX_SPEED) {
        front = 0;
        back = back + 40;
        moveBackward(back);
      }
    }
    if (incomingByte == '3') {
      if (back > front) {
        turnLeft(back);
        moveBackward(back);
      } else {
        turnLeft(front);
        moveForward(front);
      }
    }

    if (incomingByte == '4') {
      // right
      if (back > front) {
        turnRight(back);
        moveBackward(back);
      } else {
        turnRight(front);
        moveForward(front);
      }
    }

    if (incomingByte == '5') {
      // right
      front = 0;
      back = 0;
      stopRunning();
    }
  }
}

void moveForward(int speedSet) {
  motor1.setSpeed(speedSet + maxOffsetSpeed);
  motor2.setSpeed(speedSet + maxOffsetSpeed);

  motor3.setSpeed(speedSet);
  motor4.setSpeed(speedSet);
  motor1.run(FORWARD);
  motor2.run(FORWARD); // turn it on going forward
  motor3.run(FORWARD);
  motor4.run(FORWARD); // turn it on going forward
}

void moveBackward(int speedSet) {
  motor1.setSpeed(speedSet + maxOffsetSpeed);
  motor2.setSpeed(speedSet + maxOffsetSpeed);
  motor3.setSpeed(speedSet);
  motor4.setSpeed(speedSet);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD); // turn it on going forward
  motor3.run(BACKWARD);
  motor4.run(BACKWARD); // turn it on going forward
}

void turnRight(int speedSet) {
  if (speedSet < 200) {
    speedSet = 200;
  }
  motor1.setSpeed(speedSet + maxOffsetSpeed);
  motor2.setSpeed(speedSet + maxOffsetSpeed);
  motor3.setSpeed(speedSet);
  motor4.setSpeed(speedSet);
  motor1.run(FORWARD);
  motor2.run(FORWARD); // turn motor 1 forward
  motor3.run(BACKWARD);
  motor4.run(BACKWARD);
  delay(200);
}

void turnLeft(int speedSet) {
  if (speedSet < 200) {
    speedSet = 200;
  }
  motor1.setSpeed(speedSet + maxOffsetSpeed);
  motor2.setSpeed(speedSet + maxOffsetSpeed);
  motor3.setSpeed(speedSet);
  motor4.setSpeed(speedSet);
  motor1.run(BACKWARD);
  motor2.run(BACKWARD); // turn motor 1 backward
  motor3.run(FORWARD);
  motor4.run(FORWARD); // turn motor 2 forward
  delay(200);
}

void stopRunning() {
  motor1.setSpeed(0);
  motor2.setSpeed(0);
  motor3.setSpeed(0);
  motor4.setSpeed(0);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  motor3.run(RELEASE);
  motor4.run(RELEASE);
}```
\$\endgroup\$
0
\$\begingroup\$

You did not mention which version of the Adafruit Motor Shield you are using, assuming it is a V2 as the V1 is now discontinued.

Which Arduino pins are actually used by the Adafruit Motor Shield? If I want to do Serial IO through SoftwareSerial, which Digital pins are safe (free/unused) to use?

If you take a look at the Motor Shield schematics, you can find that most digital pins are up for grab except D9 and D10 which are used for the stepper motors outputs.

If I continue to use the Arduino's Tx/Rx pins instead of using SoftwareSerial via Digital ports, will that be a problem? Are those pins available for use when the shield sits on top of Arduino?

It shouldn't be a problem to use the Arduino built-in UART, according to the official documentation the main UART is on D0 and D1 which are unused on the motor shield.

Following is my code. When the robot runs, I only get one rear wheel running for almost any Serial input. Rest of the motors doesn't run. Will you please take a look at the code and see if anything looks wrong here.

The first thing I notice is wrong is the library you are using is obsolete and shouldn't be used with Adafruit's V2 version for the Motor Shield.

Why don't you take a look at their tutorial for driving DC motors with the new library?

If you are in fact using a V1 motor shield, there are a lot more pins used for driving the motors but it looks like the UART would still be free to use. They also have a tutorial for that version, have you tried simple examples without the serial logic to verify each motor can be driven correctly?

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.