MTRAS ~ MidTn Robotic Arts Society Message Board MTRAS ~ MidTn Robotic Arts Society Discussion Forum › Using PING Ultrasonic Distance Sensor and Arduino

Using PING Ultrasonic Distance Sensor and Arduino

steve g.
sghertner
Group Organizer
Nashville, TN
Post #: 123
This discussion is a continuation of posts made under our November 10th meeting Section.

Here is my recap of our posts: Frank brought his PING/Arduino project to the November 10th meeting. He wrote a program that illuminated different LED's based on movement in front of a PING sensor (very cool!). He asked how to adapt his arduino code for his PING sensor to create specific actions based on measured distances. The following website was referenced as a good example of how to use the PING sensor with an arduino:



It was also mentioned that he could do a greater than/less than type test with an if statement using the logical AND operator, &&. The code would look something like:

if (inches>45 && inches<60) digitalWrite(LED_RED, HIGH);



Robbie
user 7544054
Nashville, TN
Post #: 30
thanks for posting Steve. I was having a heck of a time trying to format code on that other page!smile
Frank C.
user 62658602
Nashville, TN
Post #: 1
so heres my code so far I would like it to switch LEDs from 0-10in. (red led) 11-20in. (blue led) and then 21-30in (green led) this is what i got.


//led major ping test
#define fP 7 //Pin the ping sensor is connected to

void setup()
{
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
pinMode(2, OUTPUT);
}

void loop()
{
if (ping() < 10);
{
digitalWrite(13, HIGH); //set led red on
digitalWrite(8, LOW); //led blue off
digitalWrite(2, LOW); //led green off
}
}


int ping() //get CM obstacle in front of sensor
{
long duration;
pinMode(fP, OUTPUT);
digitalWrite(fP, LOW);
delayMicroseconds(2);
digitalWrite(fP, HIGH);
delayMicroseconds(15);
digitalWrite(fP, LOW);
delayMicroseconds(20);
pinMode(fP, INPUT);
duration = pulseIn(fP, HIGH);
return duration / 29.0 / 2.0;
}
Robbie
user 7544054
Nashville, TN
Post #: 31
//your instructions for getting the ping duration are different from what I used

//I mostly used this code to control the PING sensor signals and input
//in the loop() cycle

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

// this converts duration to inches from the function listed below and written outside the loop() block
inches = microsecondsToInches(duration);

//then I put this outside the loop() block as a function

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.c...­
return microseconds / 74 / 2;
}


//then I could use the if inches>20 etc statements
//there was also another sketch I used for the PING that I am looking for
//to actually control the PING sensor
steve g.
sghertner
Group Organizer
Nashville, TN
Post #: 124
Frank, it looks like you are pretty close to getting it. A couple of items that you may want to look at:
1. You are converting the pulse duration into centimeters, not inches. Robbie's code shows the conversion to inches. So, you should use duration/74/2 for inches.
2. Your triggering of the ping is a little different than the datasheet and code at the arduino site. See Robbie's code above. She pulls the trigger pin low for 2 microseconds, then high for 5 microseconds, then back low. I would copy that pattern like her code.
3. Duration is defined as a long variable within ping() function but your function returns an int (integer). I don't know if your variable values are large enough to be truncated, but to keep it consistent, you should probably define ping() to return a long by declaring it as long ping(){....
4. Have you learned to print output from your program yet? It is pretty simple, just add:
Serial.begin(9600); to your setup() function
and ...
Serial.println(ping());
delay(20);
within your loop() and look at the values your program is reading from your ping sensor. You can move a piece of paper in front of your sensor and see if the values look correct.

I hope this helps.
Frank C.
user 62658602
Nashville, TN
Post #: 2
Thx ill try it out tomorrow.
steve g.
sghertner
Group Organizer
Nashville, TN
Post #: 125
Let us know how it goes.
steve g.
sghertner
Group Organizer
Nashville, TN
Post #: 126
Frank, Did you see this project on the Make website? It is similar to what you are doing...

http://makeprojects.c...­
Powered by mvnForum

People in this
Meetup are also in:

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy