sketching PING to control motor using motor sheild

Hi there! I'm trying to write a sketch that would enable a Parallax PING sensor to turn off a motor that's wired through the Adafruit Motor shield when it detects an object 2 ft away. I plane to have two PING sensors attached to the front of a boat and as the boat gets close to the side of my pool, the PING on the right stops the motor on the left allowing it to turn away. then once its clear to start again and resume. I hope this is the correct topic to ask about this in considering its about code. Thanks in advance for any help :)

So, show us what you've written and then we can help.

Ive been trying so far to Frankenstein code Ive found but no life yet!!! lol

//Ping motor control

#include <AMotorC.h>

#include <AFMotor.h>

const int PingSensor1 = 4; // Defines ping sensor 1

AF_DCMotor motor1 (1, MOTOR12_1KHZ); //Creates motor 1 at 1khz



void setup()
{
  Serial.begin(9600);// initialize serial comms
}


 

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  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(PingSensor1, OUTPUT);
  digitalWrite(PingSensor1, LOW);
  delayMicroseconds(2);
  digitalWrite(PingSensor1, HIGH);
  delayMicroseconds(5);
  digitalWrite(PingSensor1, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(PingSensor1, INPUT);
  duration = pulseIn(PingSensor1, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
  
float distance = readPingSensor1();  // read the sensor and convert into feet
{
   if (distance > 3)  // more than 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(255);  // full speed
      }
   else if (distance > 2)  // between 2 and 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(127);  // hal;f speed
      }
   else  // closer than 2 feet
      {
      motor1.run(RELEASE);  // stop motors
      }
}
// 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(PingSensor1, OUTPUT);
  digitalWrite(PingSensor1, LOW);
  delayMicroseconds(2);
  digitalWrite(PingSensor1, HIGH);
  delayMicroseconds(5);
  digitalWrite(PingSensor1, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(PingSensor1, INPUT);
  duration = pulseIn(PingSensor1, HIGH);

Do you see in this section, that everything refers to "PingSensor1". So, if you wanted to add a second sensor, you'd need to replicate the code. A better approach would be to write a simple function that took as a parameter the number of a pin to which is attached a Ping sensor. Then, the same function would return the duration of the Ping pulse.

This

float distance = readPingSensor1();  // read the sensor and convert into feet
{
   if (distance > 3)  // more than 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(255);  // full speed
      }
   else if (distance > 2)  // between 2 and 3 feet
      {
      motor1.run(FORWARD);
      motor1.SetSpeed(127);  // hal;f speed
      }
   else  // closer than 2 feet
      {
      motor1.run(RELEASE);  // stop motors
      }
}

is confusing. It looks like a function definition that has morphed into a call to a function. If you're not sure how a function defintion should look, read the function "microsecondsToCentimetres", and then look at how (and where) it might be called.

Boy I remember seeing 2001 at the movie theaters back in the day! lol
I love the fact that now almost everyone has a H.A.L.E. in their home! :slight_smile:

Sorry! I know it looks bad! I've had a total of one C programming classe but I do have another in the works right now! I've only been coding for about 6-7 months! I guess I just bit off more then I could chew right now but damn I've been having fun building this thing! Ive been studying all the examples that seem to apply to this project but putting them together is another story! I guess the forums can't help till I know more! Any good books on sketching you could recommend? Ive done all the basic projects and have the beginning arduino books. :)