Programming question - PING and motor control - I am missing something.

Hello everyone -

My son and I are trying to run a 4 motor rover with the Arduino Duemilanove and the AdaFruit motor shield, with input from the Parallax Ping sensor. The motors work well and respond properly to the Motor Runner sketch provided by Adafruit, and the Ping operates properly with the Ping sample sketch. I am trying to get the rover to move forward until an obstacle of x distance is in front of it, and then stop. When I mix the two sketches (as attached), the PING works properly, but the motors REFUSE to move as long as the motor.run commands are within the if/else loop. I know that the if/else loop is working properly because the Serial Monitor returns the correct “Stopped” or “Go, go, go!” messages, dependent on the distance read by PING. If I move the motor.run command to immediately after the void loop command, it works, but nowhere else. What am I missing here? Any assistance would be great.

#include <AFMotor.h>
const int pingPin = 7;
const int threshold = 1000;  // threshold of distance in miliseconds

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #3, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #4, 64KHz pwm

void setup() {
  Serial.begin (115200); 
  motor1.setSpeed(255);     // set the speed to 150/255
  motor2.setSpeed(255);     // set the speed to 150/255
  motor3.setSpeed(255);     // set the speed to 150/255
  motor4.setSpeed(255);     // set the speed to 150/255
}

void loop () {
 
  
  long duration, inches, cm;
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(5);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  
   pinMode (pingPin, INPUT);
  duration = pulseIn (pingPin, HIGH);
  
  Serial.print(duration);
  Serial.println();
  
if (duration < threshold) { //if the distance is  less than 1000 miliseconds (approximately 6 inches), stop the motors

  motor1.run(RELEASE);      // stopped
  motor2.run(RELEASE);      // stopped
  motor3.run(RELEASE);      // stopped
  motor4.run(RELEASE);      // stopped
  Serial.print("Stopped");
  Serial.println();
  delay (1000);
}

else  {  //if the distance is greater than 1000 miliseconds or approximately 6 inches, move forward 6 inches.

  motor1.run(FORWARD);      // turn it on going forward
  motor2.run(FORWARD);      // turn it on going forward
  motor3.run(FORWARD);      // turn it on going forward
  motor4.run(FORWARD);      // turn it on going forward
  Serial.print("Go, go, go!");
  Serial.println();
  delay(1000);


} }

Ping_and_Motors.ino (1.61 KB)

From the Adafruit page about this shield:

The following pins are in use if any DC/steppers are used

Digital pin 4, 7, 8 and 12 are used to drive the DC/Stepper motors via the 74HC595 serial-to-parallel latch

Bad choice for pingPin, it seems.

Also:

What pins are not used on the motor shield?

All 6 analog input pins are available. They can also be used as digital pins (pins #14 thru 19)

Digital pin 2, and 13 are not used.

Thank you SO much! It works perfectly now. :slight_smile:

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1, 64KHz pwm
  Serial.begin (115200);

These two statements do not go together, either. The Serial pins are 0 and 1.

That’s motor 1 Paul, not port D1. Or in other words, connector 1 (out of 4) on the shield, not the Arduino.

That's motor 1 Paul, not port D1. Or in other words, connector 1 (out of 4) on the shield, not the Arduino.

Oh. OK.