Skip to main content
edited body
Source Link

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 (!(PINDPINB & _BV(PB5)))
  ; // wait for leading edge
up = millis();
while (PINDPINB & _BV(PB5))
  ; // wait for trailing edge
down = millis();
Serial.println(down - up);

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 (!(PIND & _BV(PB5)))
  ; // wait for leading edge
up = millis();
while (PIND & _BV(PB5))
  ; // wait for trailing edge
down = millis();
Serial.println(down - up);

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

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 (!(PIND & _BV(PB5)))
  ; // wait for leading edge
up = millis();
while (PIND & _BV(PB5))
  ; // wait for trailing edge
down = millis();
Serial.println(down - up);