Hi,
I was able to measure distance using HC-SR04 sensor via C code. (pulseIn function).
Now I want to try this using Python. I see that pyfirmata doesn't seem to have this nice "pulseIn" function that returns the duration in microseconds. So it seems that I have calculate myself... yet it doesn't seem to work... Anyone with any success?
I am using an UNO board and loaded standardFirmata sketch. And connected via MacBook USB to UNO USB.
I'm using
pin 12 => trigger
pin 8 as Echo (tried 11 as well but same result).
PORT = '/dev/tty.usbmodem411'
board= Arduino(PORT)
it = util.Iterator(board)
it.start()
sonarEcho = board.get_pin('d:8:i')
sonarTrig = board.get_pin('d:12:o')
for i in range(10):
sonarTrig.write(0)
time.sleep(1)
sonarTrig.write(1)
time.sleep(0.000010)
sonarTrig.write(0)
##### direct write doesnt work either=> board.digital[12].write(0)
while sonarEcho.read() is None:
pass
t1= dt.datetime.now()
while sonarEcho.read():
pass
t2=dt.datetime.now()
t3=(t2-t1).microseconds
distance=t3/58
print'Distnce:',distance, 'cm:'