Reading ultrasonic pulse

This question is part of another question, but as I lost so much time into this I decided to open this thread so it does not get lost in the other question that I mixed with it.

I am trying to read my ultrasonic sensor. It sends a pulse in the trigger pin and the echo pin receives the pulse. Using pulseIn I can know exactly the time it took to the pulse go and back. But I need to use port register which is much faster and I need it to be fast in this specific case.

My code is very simple:

void setup() {

Serial.begin(9600);
//pin 12 as output
DDRB = B00010000;
//pin 12 as low
PORTB = B00000000;

}

void loop() {

//pin 12 as high
PORTB = B00010000;
delayMicroseconds(10);
//pin 12 as low
PORTB = B00000000;

long start_time = micros();

//While reading high in pin 13 keep running the loop
while (PINB & B00100000)) {

}

Serial.println(micros() - start_time);

delay(1000);

}
The problem is that it does not work, I mean, the while loop is not being processed so the serial is always printing a very small number. It doesnt matter if I move the ultrasonic sensor to distant walls the while loop still does not work. What am I doing wrong?

From http://arduino.cc/en/Tutorial/DigitalPins

NOTE: Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that’s soldered to the board on most boards. If you enable its internal 20k pull-up resistor, it will hang at around 1.7V instead of the expected 5V because the onboard LED and series resistor pull the voltage level down, meaning it always returns LOW. If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor.

//While reading high in pin 13 keep running the loop
    while (PINB & B00100000)) {

You don't wait for the pin to become HIGH.

Why another post?

Don't remove threads.
When you have a new topic, then you create a new thread.
Don't worry about a thread getting too big.
As long as it is on the same topic keep things in the same thread.

gilperon:
AWOL why? The pin is inittially LOW so I suppose that when the ultrasound gets the sensor it will be high, isnt?

But your wait loop is while the pin is HIGH, but you don't wait for it to become HIGH first, so if it is LOW to begin with, the body of the loop will never be executed.

You're the one with the means of testing in front of you - why not try it?

The Arduino way to do it is:

void setup() {
    Serial.begin(9600); 
    pinMode(12, OUTPUT);
    digitalWrite(12, LOW);
}

void loop() {
    // Pulse Pin 12
    digitalWrite(12, HIGH);
    digitalWrtite(12,LOW);

    //  58.2 microseconds per cm (round trip time).
    unsigned long echoTime = pulseIn(13, HIGH, 23280);  // Timeout at 4 Meters (400 cm * 58.2 uS)

    Serial.print(echoTime);
    Serial.print(" uS = ");
    Serial.print(echoTime/58.2);
    Serial.println(" cm");

    delay(1000);
}

Hi,

What's wrong with a bit of simple code? I find it works every time, like this:

//------------------------------------------------------------------
long scanner(long cm)
{
	const int pingPin=7, EchoPin=8;
	long duration;

	// The SRF005 is triggered by a HIGH pulse of 2 or more microseconds.
	// Give a short LOW pulse before to ensure a clean HIGH pulse:
	pinMode(pingPin, OUTPUT);
	pinMode(EchoPin, INPUT);  

	digitalWrite(pingPin, LOW);
	delayMicroseconds(2);
	digitalWrite(pingPin, HIGH);
	delayMicroseconds(2);
	digitalWrite(pingPin, LOW); 
	duration = pulseIn(EchoPin, HIGH);
	delay(100);

	// convert the time into a distance
	// inches = microsecondsToInches(duration);
	cm = microsecondsToCentimeters(duration);
	return (cm);
}
//------------------------------------------------------------------
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;
}
//------------------------------------------------------------------

Hope it helps.
Regards

Mel.

it will only go HIGH when the sensor got ultrasonic data

No, it will go high shortly after the sensor is triggered (probably about the same time as the eight cycle pulse train is generated), and will go low as soon as an echo is received.
Note the use I made of the word "shortly".

As I mentioned trigger pulse to ECHO pulse ~460uS.

One of these posts should be Locked.