Yes, you can easily do what you want to do.
This code is what I am using for my wifitank, to measure the distance to things in front and behind it, so it won't drive into things.
double ping(int outPin, int inPin) //Get CM to obstacle in front of the sensor
{
long duration;
pinMode(outPin, OUTPUT);
pinMode(inPin, INPUT);
digitalWrite(outPin, LOW);
delayMicroseconds(2);
digitalWrite(outPin, HIGH);
delayMicroseconds(15);
digitalWrite(outPin, LOW);
delayMicroseconds(20);
duration = pulseIn(inPin, HIGH, 10000); //10000 timeout to make sure the loop wont slow down too much
return duration / 29.0 / 2.0;
}