• Hi Max,

    the way I understand you code is that every 50[ms] the sensor is sent a pulse of 10[ms] via Pin A0. Pin A1 listens for a rising and a falling edge with on ('inline', anonymous) callback (functions) each. The callback for the rising edge catches t1, and the callback for the falling one gets the time delta. With the time delta and some formula the dist(ance) is calculated. The distance is divide into three bands and each of these bands is assigned to one of the onb-board LED and to be lit when in respective distance band.

    I wonder about the timings... do you have some figures?

    330[m/s] is the speed of sound.
    To 'travel' 0.1..0.2[m] it takes between 0.3..0.6[ms]... which would show in dt (0.0003..0.0006[s]).... and so one for the the other distances. Any of these rises and falls happen in a fraction of your digital pulse...

    I did some setWatch on pin(s) here and here... but on way way 'slower' terms...

    What I can imagine is that the times make it fail... even though Espruino has an event cache to capture event bursts and lets you process them on low activity... and your 20 times a second give ample such low activity phases.

    I'm now going a bit on a limb, but did you consider these things:

    a) take the time before the pulse, have only one watch - the falling watch - going, and then figure out with calibration exercise the terms to adjust the measured time... because with such short times of fractions of [ms] js code interpretation and execution are noticeable...

    b) put rising and falling on different pins... (don't know though what difference that would make... but I know that there are restrictions in using the setWatch on pins).

    Also what I wonder if your sensor is actually properly triggered... if you do not set the pin mode to output, the TRIG pin will be in three-state after finishing the pulse... and depending on the sensor's internals, this could mean that there is actually no sound going out... or actually always, as soon power is on.

    Can you verify that the sensor is actually quite, then 10ms sounding, and then quiet again? Try to set the pinMode to output.

    Looking forward to hear from your success...

About

Avatar for allObjects @allObjects started