0

Run 2 motors forwards, V1 Shield
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Run 2 motors forwards, V1 Shield

by jimmyeao on Fri Jan 25, 2019 5:29 am

Hi All,
Im pretty new to all this, trying with my daughter to get a SMARS robot running using an UNO and V1 Motor Shield.
We have 2 motors and an ultrasonic detector hooked up. Im struggling to get it to run both motors at the same time forwards, but ironically it does both when it detects an object and needs to turn. I assume I have made a simple error somewhere:

Code: Select all | TOGGLE FULL SIZE
#include <AFMotor.h>


//ultrasonic setup:
 int distancecm=0;
 const int trigPin = 7; // trig pin connected to Arduino's pin 10
 const int echoPin = 6; // echo pin connected to Arduino's pin 11
// defines variables
long duration;
int distance;

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor with ultrasonic sensor!");
motor1.setSpeed(100);
motor2.setSpeed(100);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
delay(1000);

}

void loop() {
 
 distancecm=mdistance();   
  if(distance<5){
    Serial.println("Object Detected/n");

motor1.run(BACKWARD);
motor2.run(BACKWARD);

    delay(1000);


    motor1.run(FORWARD);
    motor2.run(BACKWARD);

    delay(1000);

  }
  else{

    Serial.println("Moving Forwards/n");
    motor1.run(FORWARD);
    motor2.run(FORWARD);

  }

}

//ultrasonic distance mesurement function
int mdistance()
{


digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print(distance);

   
  }

jimmyeao
 
Posts: 3
Joined: Fri Jan 25, 2019 5:20 am

Re: Run 2 motors forwards, V1 Shield

by adafruit_support_bill on Fri Jan 25, 2019 7:07 am

For most 2-motor robots, the motors are mounted on opposite sides and need to run in opposite directions for forward motion. You can handle this in your code by telling them to run in different directions. Or you can simply reverse the wires on one of the motors where it connects to the shield.

adafruit_support_bill
 
Posts: 71765
Joined: Sat Feb 07, 2009 10:11 am

Re: Run 2 motors forwards, V1 Shield

by jimmyeao on Fri Jan 25, 2019 7:19 am

adafruit_support_bill wrote:For most 2-motor robots, the motors are mounted on opposite sides and need to run in opposite directions for forward motion. You can handle this in your code by telling them to run in different directions. Or you can simply reverse the wires on one of the motors where it connects to the shield.


Thanks for the tip - however only one motor runs where I have them both set to run "Forwards"

jimmyeao
 
Posts: 3
Joined: Fri Jan 25, 2019 5:20 am

Re: Run 2 motors forwards, V1 Shield

by jimmyeao on Fri Jan 25, 2019 7:29 am

Well this is embarrassing... one of the wheels was rubbing enough to prevent the motor from turning! thanks :)

jimmyeao
 
Posts: 3
Joined: Fri Jan 25, 2019 5:20 am

Re: Run 2 motors forwards, V1 Shield

by adafruit_support_bill on Fri Jan 25, 2019 8:09 am

Good to hear it is working. Thanks for the follow-up.

adafruit_support_bill
 
Posts: 71765
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.