user4924: 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?

user368: According to anything I could find, the Echo output is normally low and then goes high for a duration equal to the range. Your initial while condition is inverted, and you need to check for both edges.
unsigned long up, down;

// trigger pulse
while (!(PINB & _BV(PB5)))
  ; // wait for leading edge
up = millis();
while (PINB & _BV(PB5))
  ; // wait for trailing edge
down = millis();
Serial.println(down - up);


4924: <p>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.</p>

<p>My code is very simple:</p>

<pre><code>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 &amp; B00100000)) {


    }

    Serial.println(micros() - start_time);

    delay(1000);

} 
</code></pre>

<p>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?</p>
 368: <p>According to anything I could find, the Echo output is normally <em>low</em> and then goes <em>high</em> for a duration equal to the range. Your initial while condition is inverted, and you need to check for <em>both</em> edges.</p>

<pre><code>unsigned long up, down;

// trigger pulse
while (!(PINB &amp; _BV(PB5)))
  ; // wait for leading edge
up = millis();
while (PINB &amp; _BV(PB5))
  ; // wait for trailing edge
down = millis();
Serial.println(down - up);
</code></pre>
