NEED HELP! Coding.

I am making a device using the Ping))) sensor from Parallax and I am using the provided code on the Arduino website: /* Ping))) Sensor

This sketch reads a PING))) ultrasonic rangefinder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor.

The circuit: * +V connection of the PING))) attached to +5V * GND connection of the PING))) attached to ground * SIG connection of the PING))) attached to digital pin 7

http://www.arduino.cc/en/Tutorial/Ping

created 3 Nov 2008 by David A. Mellis modified 30 Jun 2009 by Tom Igoe

This example code is in the public domain.

*/

// this constant won't change. It's the pin number // of the sensor's output: const int pingPin = 7;

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

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(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, 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(pingPin, INPUT); duration = pulseIn(pingPin, 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; }

I need to modify this code. Instead of using one Ping)) sensor, I am using 3 with each one hooked up to vibration motor. As the distance gets smaller, the vibrations will get occur more often until the distance is so close that it is rapidly buzzing. Can someone please help me modify this code?

The Ping example is just about the worst example on the playground. All the stuff for dealing with the ping sensor should have been put in a function that takes one argument (the pin) and returns the distance, in inches or in centimeters.

long pin(int pingPin)
{
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(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, 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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  return inches;
}

Change the last two lines if you prefer centimeters.

Then, loop looks like:

void loop()
{
  long dist = ping(7);
  Serial.print(inches);
  Serial.println("in");
  delay(100);
}

With this code, it is easy to see how to add another sensor on another pin.

void loop()
{
  long dist = ping(7);
  Serial.print(inches);
  Serial.println(" in");
  delay(100);

  dist = ping(8);
  Serial.print(dist);
  Serial.println(" in");
  delay(100);

  dist = ping(9);
  Serial.print(dist);
  Serial.println(" in");
  delay(100);
}

Of course, the delays aren't necessary, nor is it necessary to print the data to the serial port.

Instead of using one Ping)) sensor, I am using 3 with each one hooked up to vibration motor.

I hope that you mean that each ping sensor controls one vibration motor...

Thank you first off and yes I mean each Ping))) controls one vibration motor. But how do I go about coding for the vibrations?

But how do I go about coding for the vibrations?

Tell us something about the vibration motors. How are they connected to the Arduino? Are you trying to control the speed of the motor? Using PWM?

If so, I'd suggest that you want to map the distance range of interest to a PWM value.

long dist = ping(somePin);
constrain(dist, 0, 30); // Keep the distance between 0 and 30 inches
int pwmVal = map(dist, 30, 0, 0, 255); // map long distances to smaller values
analogWrite(motorPin, pwmVal);

My thoughts:

  • I would like to use PWM
  • I want to increase the speed of the vibration motors as the distance decreases or I want to have periodic buzzes and as the distance decreases the buzzes get periodically faster
  • I want to use cm and I don’t care whether if its displayed on the monitor, all I want is the 3 vibration motors and their respective Ping))) sensor to work together
    [/list]

If I could get some finalized code, that would be great.

If I could get some finalized code, that would be great.

It might, but what would you have learned?

I would like to use PWM

Don't you know whether you can?

Haha. As you probably have guessed, I’m a novice. I know how to code java but I need this too learn more. I’m a better learner through viewing code rather than doing it myself haha.

. I'm a better learner through viewing code

There's code already over at the playground, or here on the forum, just go look for it.

Really, I haven't found anything over vibration motors and Ping))). Would it be possible for a link?

You got the Ping bit sorted, so it is just down to driving the motors.
Phone vibras can be deceptively juicy, so make sure you use some form of driver.

Yeah thats the main part But I also have no idea on how to implement the motors and how to make them communicate with the Ping))) sensors. Help would be appreciated. Thank you.

The motors do not (cannot) communicate with the Ping (or vice versa). That's what the Arduino is there for.

Help would be appreciated. Thank you.

No, really, it's your turn. You need to tell us about the motors that you are using.

http://www.sparkfun.com/products/8449 Would these work? Once again, I thank you both.

Rated Current [mA] 75

75mA is more than a digital pin can supply directly. You would need to use a transistor to turn that on, with an external power source.

Oh really. I can't find any other vibration motors. How much can the digital pin supply?

Ok so

DC Current per I/O Pin 40 mA DC Current for 3.3V Pin 50 mA

There are a bunch of motors here: https://secure.precisionmicrodrives.com/vibration_motors.php?_kk=vibration%20motor&_kt=60b501a9-04c0-4fb2-8741-0af634a623ba&gclid=COGvwtSDmqcCFZNd7Aodwk_OcA

Can I use any?

If they use very much less than 40mA, yes. You can use virtually any of them with a simple external transistor, diode and resistor.

That is true. I don't think i need to worry about that now. I need to write the code.

I don't want to sound crass. But can someone please help me write?