Code for PING sensor

Hi,

I'd like to set-up a PING sensor in a doorway, so that it triggers a relay when someone enters the room. I'm sure someone must have done something similar, but I haven't been able to find the right code online. Also, is it possible to set a different output depending on which way someone walks past the sensor. For example; when someone walks into the room it switches one relay and when they walk out it switches a different relay? Or would I need two sensors for that?

Can anyone help with code, or point me in the right direction?

Thanks,

Ben.

I have done something similar to this before. Unfortunately I do all my coding on a netbook, which I forgot at my folks' house when I visited a couple months ago. But, what I did was use the sample PING code from the Arduino web page and then measured the width of my door frame with the PING in the serial monitor. Then I subtracted one inch from that and added an if loop in my code asking if the the variable that's being printed to the serial monitor was less than that value. If so, execute case 1 (which for you is making the servo do whatever) and if not execute case 2 which is to keep looking if the value is less than (door width - 1).

You might be able to detect direction of travel if you have two sensors pointing outward and inward above the door frame. Typically people use IR sensor on reflective setup for this type of application. It's like the door chime in small shops. If you have two of them you may find direction of travel with the sequence of the IR sensor being triggered.

Hi Guys,

Thank you for the replies. Sorry it’s taken me a while to get back; I wanted to try and figure it out from your advice before asking again.

I’m not at all experienced at writing code, so I’m struggling. I’ve tried adapting the PING example to (at this point) switch an led if the distance is shorter than the doorway. I’ve also tried removing the serial port command because I want it to run without a computer. It’s for an installation, which will activate a DVD player when the viewer enters the space. I’ll have relays replacing the Play and Stop switches of the machine.

Could you take a look at my code please. I’ve written an ‘if’, ‘else’ statement, but am getting expected unqualified-id before ‘else’.

Thanks,

Ben.

*/

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

void setup() {
pinMode(13,OUTPUT);

}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches;

// 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);
if (duration < distance);
digitalWrite (13,HIGH);
}
else {
digitalWrite(13,LOW);
}

What you’ve got with the closing brackets puts the else outside the loop function. The semi-colon on the if line is not helping either. This:

if (duration < distance);
  digitalWrite (13,HIGH);
}
else {
    digitalWrite(13,LOW);
  }

Should really be:

if (duration < distance)
  {
  digitalWrite (13,HIGH);
  }
else
 {
    digitalWrite(13,LOW);
  }
}

Hi Wildbill,

Thanks. That compiled and uploaded fine, but I’ve just got a constantly flickering led, unless I place something within about 16 inches of the sensor, in which case the led stays off, and the activity led on the PING stays on. So is my

const int distance = 33;

and

if (duration < distance)

wrong?

I’m trying to tell the led to switch on if the sensor detects an object less than 33 inches away.

Thanks for your help,

Ben.

Try putting a Serial.println(duration); in before the if and see what you're getting from the ping.

Have you tried a small delay of say 20 milliseconds between readings?

Ping is running perfectly according to the serial monitor, but since I’ve added this back in, I’m getting nothing at all from the led, just a rapidly flashing activity led on the sensor.

const int pingPin = 7;
const int distance = 33;

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

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);

if (duration < distance)
{
digitalWrite (13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
}

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;
}

Since you are testing duration in the if:

Try putting a Serial.println(duration); in before the if and see what you're getting from the ping.

Sorry, didn’t understand the first time. I think that’s fixed though.

Adding ‘serial.println(duration)’ gave me a value of approx. 4850 with an object at 32inches from the PING, so I changed

‘if(duration < distance)’ to ‘if(duration < 4850)’. I guess that’s what you were getting at.

Seems to have cracked it. Means I can start thinking about the relays.

Thanks all for your help.

Ben.

Glad to hear you resolved it. Given that you're now calculating the range in inches for the print, you might consider testing against that in your if instead. 4850 isn't a very meaningful number, at least compared to 33 inches.

Thanks, Wildbill. I’ll try

if(duration / 75 < 32)

Correction of my error for anyone having similar issues that might use this code.

That should be:

if (duration / 74 / 2 < 32)

Duration in microseconds divided by 74 gives ms per inch. Halve this value to give the distance of the object.

I noticed you didn't name the led and the pin it is attached to before the void Setup(). Have you tried that? That may be why the led isn't working.