[SOLVED]Can't control motors using distance gauged by ultrasonic sensor

I'm using the Adafruit motor library to control 2 DC motors, which I can do perfectly fine without running my PING))) ultrasonic sensor. I can confirm that the ultrasonic sensor works perfectly when using it by itself. I can also control servo motors based on an if/else statement using the distance in inches taken from the PING))) sensor. When I replace that servo motor code with the DC motor code(which I'll add ran by itself perfectly fine in a sketch) nothing happens. What is wrong with my code? I'm sure I'm providing enough current through the board. It's not a hardware problem since everything else works on its own through separate sketches. And I can control servo motors using this code(in the if(inches<x){} statements), but not the DC motors. What could be wrong? This is my code.

#include <AFMotor.h>
#include <Servo.h>

const int pingPin = 8;

Servo sServo;
AF_DCMotor motor(4);
AF_DCMotor motor2(1);
//motor 2 has opposite direction(eg. backward = forward & vice versa)
  long microsecondsToInches(long microseconds){
    return microseconds / 74 / 2;
  }
  
  long microsecondsToCentimeters(long microseconds){
    return microseconds / 29 / 2;
  }
void setup(){
  sServo.attach(9);
  Serial.begin(9600);
}  

void loop(){
  long duration, inches, cm;
  pinMode(pingPin,OUTPUT);
  digitalWrite(pingPin,LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin,HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin,LOW);
  
  pinMode(pingPin,INPUT);
  duration = pulseIn(pingPin,HIGH);//measures duration
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  if(inches<2){
    motor.run(RELEASE);
  }
  
  if(inches>2){
    motor.run(FORWARD);
    motor.setSpeed(255);
  }
  
  }

I assume that you are getting sensible values displayed on the serial monitor.

AF_DCMotor motor2(1);

Does this mean that motor2 uses digital pin 1 ? If so then it won't work because the serial interface uses that pin on most Arduino boards.

I'm sure I'm providing enough current through the board

I don't like the sound of that. How are the motors powered ? Direct from the Arduino or from an external source with a common GND ? Do you still have a servo attached and, if so, how is that powered ?

No, it's not attached to pin 1. It's attached to a separate pin on the motor shield specifically for motors. I'm powering the motors through a 9V battery. The servo is powered through the board.

I'm powering the motors through a 9V battery.

One of those smoke detector batteries? If so, you should be ashamed of yourself.

The servo is powered through the board.

Not a good idea, either. Servos suck a lot of current.

Sorry, I meant to say 6V. I'm using four 1.5 V batteries. And I'm not attaching the servo when I'm using the DC motor. And can you please tell me what's wrong with using a 9V battery?

Nine volt batteries ( the rectangular kind ), actually have several very small cells in them, they are not capable of supplying the amount of current that would drive any kind of electric motor.

Oh ok thanks for giving a reason. I'm not using them, anyway. And the motor requires 6V. Is there any reason why my program shouldn't work? The PING))) sensor works separately and the motors work separately. I'm not so sure I'm providing enough current anymore, but is that the only possible problem? How do I fix this?

Is there any reason why my program shouldn't work?

Any number of reasons.

AF_DCMotor motor(4);
AF_DCMotor motor2(1);

Motor number 4 is called motor. Motor number 1 is called motor2. Why?

Do the numbers of the motors correspond to motor numbers on the shield? Just for giggles, which shield is it?

  cm = microsecondsToCentimeters(duration);

You don't do anything with this value beyond printing it. Why bother computing the value? Why bother printing it? Why bother having the function to compute the useless value?

A link to the shield and a photo of your setup are going to be necessary in order to help you.

I uploaded the image here. http://postimg.org/image/ikm9f7zpp/ The red and black wires are attached to pin M4 of the motor shield and are connected to the motors below. I don't have the servo motor attached at the same time as the 130-size dc motor. If anything, the dc motor draws more current than the servo motor. I'm using this dc motor. This is the servo motor. According to the links, the dc motor draws more, but I'm providing enough current. This is the ultrasonic sensor. Is there any other way I can fix this problem?

I called the motor "motor". It's attached to the m4 on the OSEPP motor shield. I'm going to put up a picture. This is a link to the motor shield.
http://osepp.com/products/shield-arduino-compatible/motor-servo-shield/
I really appreciate your help. I need this for a science project. The DC motors can run while I'm running the ultrasonic sensor, but not at the same time as the ultrasonic sensor. What else could be the problem?

I called the motor "motor". It's attached to the m4 on the OSEPP motor shield.

Then, perhaps m4 would have been a better name. Or leftMotor.

I don't have the servo motor attached at the same time as the 130-size dc motor.

The motor link shows that the motor has an 0.8 amp stall speed. The motor shield provides up to 0.6 amps of current before smoke becomes an issue. Not a good match at all.

The DC motors can run while I'm running the ultrasonic sensor, but not at the same time as the ultrasonic sensor.

I don't understand what "running the ultrasonic sensor" means. Start a motor running, and it should stay running while the ping sensor is doing its thing.

0.8 amps is the stall current. Isn't that when it has a lot of load? Ok. I'll change the motor name. When I start running the motor before the ultrasonic sensor, it immediately stops after I write to the pingPin(that or something else in the ping))) code). This is my problem.

Is there anything else I can do? I really need to get this working. I see no reason why this code works with just the servo motors and not the DC motors.

Yes, go back to the sketch that worked with just the motors and check that still works,
if so its a software issue perhaps, carefully add back ping functionality.

If that doesn't work its likely hardware, multimeter time, check all the voltages are as
expected, that the pins respond to the code, that the motor outputs change when expected.

Continuity check all your wiring - you are using hook-up wires for your motors, not a
great idea, those hook up wires are very thin and will struggle with motor levels of
current...

Check the schematic of that motor shield to make sure you aren't using one of the pins
it uses for something else (the ping sensor).

it immediately stops after I write to the pingPin(that or something else in the ping))) code). This is my problem.

You need to look at which pins the motor shield is using. You can not use those same pins for other purposes. I don't know that you are doing that, but something isn't right.

I'm using pin 8 for the ultrasonic sensor and the m1 pin on the motor shield.

gwin:
I'm using pin 8 for the ultrasonic sensor and the m1 pin on the motor shield.

Yes and pin 8 is used by the motor shield for the DIR_SER signal.
Look at the schematic of the motor shield and see what pins are free.

Pin 2 and 13 look free along with all the analogue pins.

I'm using pin 8 for the ultrasonic sensor and the m1 pin on the motor shield.

So, which feature do you want to work? Both is the wrong answer.

I want to be able to stop the motor depending on the distance of an object from the ultrasonic sensor(the if/else statements with the inches in the code). The motors just don't run at all with the code I have. They will run while the ultrasonic sensor isn't running(is off), and vice versa for some reason. I can control a servo motor using the servo library with the code I have right now. I just can't do that with the Adafruit motor library for some reason. I can run the motors with this same code if I remove all the PING))) sensor code, though.

THANKS! It worked when I used pin 2.