I'm making a library for SRF04/HC-SR04 I'm missing something (maybe it's there but somehow I can't find it).
This ultrasonic sensor that works as follows (most works like this, indeed):
In Arduino you typically do:
Code: Select all
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH);
cm = duration/29/2 /* For inches: /74/2 */
Code: Select all
trigger = pyb.Pin(pyb.Pin.board.X3)
trigger.init(Pin.OUT_PP, pull=Pin.PULL_NONE)
trigger.low()
echo = pyb.Pin(pyb.Pin.board.X4)
trigger.init(Pin.IN, pull=Pin.PULL_NONE)
pyb.udelay(10) # Just a little wait.
trigger.high()
pyb.udelay(10) # Mantain pulse for 10 useconds.
trigger.low()
# And then the part that is wrong wrong wrong! I'm meauring milliseconds...
while not echo.value():
continue
start = pyb.millis()
while echo.value():
continue
end = pyb.millis()
cm = (start-end) / 29 / 2
I'm probably missing something...
Any hint?