I think you got your numbering wrong there in the comment, because P8_08
is bela_digitalIn12
.
So first thing I would check the pinout.
Second, you want to make sure you are receiving your digital inputs. Try to connect P8_08
(or whatever input you are using (assuming it is properly init'ed)) directly to 3.3V (P9_3
) and that should print 1
to the console.
Once you get to that point, make sure you are receiving data from the sensor. After you send a pulse, you should receive a pulse, that is a 1
followed by a 0
.
The PulseIn
C++ class checks for the duration of a pulse on the digital input (see the relevant implementation here).
So you only need to compute the time difference between the 1
and 0
messages (e.g.: with a [select 0 1]
and [timer]
) and that will give you the length of the pulse in ms
. The datasheet of the HCSR04 (written in a very bad English) then gives the formula to turn that into cm
.
If you still do not receive any signal on the echo pin, then first thing make sure you are using the appropriate scaling resistors (we discussed this here).
Then, it occurs to me that the datasheet states
"Using IO trigger for at least 10us high level signal
In the example code we wrote it is about $44\mu s$
and it worked fine.
In your case it is $\Delta_t = 0.01 / 16.66 = 500\mu s$
I am not sure if this is ok for the device (the datasheet does not give a maximum value), but try to make it smaller by, e.g.: changing [-~ 0.99]
to [-~ 0.999]
. Accordingly, change [*~ 100]
with [*~ 1000]
(no harm in multiplying by 1e7
or something that huge: the output will always be clipped before being sent to the dac).
Note: you should not need the disable
in the unused digital channels, as they are disabled by default.