Beginner playing with HC-SR04 / General how to start question #4512
Replies: 1 comment
-
Posted at 2015-03-13 by @allObjects 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. 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... Posted at 2015-03-13 by @allObjects Regarding learning JavaScript... what other (programming) languages are you familiar with? Since JavaScript source is never really 'hide-able', learning from any existing source is the most powerful... but can be confusing too, because there are so many styles and personal preferences... Choosing a good model with separation of concerns if always helpful... and so is decent encapsulation // oo-thinking... with Espruino there are though some limits given because of the (memory) resource constraints. Nevertheless, actual code implementation can vary, but still can conceptually stick to a clean state and behavior... Posted at 2015-03-14 by DrAzzy My thought would be
Maybe cleaner:
(code is untested) (that's assuming you've checked that the dist values you're getting are sensible) Posted at 2015-03-14 by Kaermorhen Thanks a lot DrAzzy with
added on i have what i want change of a led during movement of my hand, previously i messed up with brackets "{" after else. currenty i'm using DMIS in my work, but it isn't a lot of programming to do, due to the fact that there are a lot of ready to go functions to use in software (PC-DMIS) - I think i try learning Java on code.org for learn how to don't stuck by syntax. I'm impressed of how fast Espruino forum works. Posted at 2015-03-14 by DrAzzy Yay! It is important to bear in mind that Java and JavaScript are different, despite the similar name, and in fact aren't much alike. (JavaScript is way hotter than Java right now, by the way, just in terms of general usefulness of knowing how to use the language. There is a HUGE amount of information on javascript online, though it's almost all in the context of webpages - still all useful for syntax. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2015-03-13 by Kaermorhen
Hi,
Finally I have bought some hardware to my espruino boards and I have a question:
I started with code from distance sensing robot:
i'm trying to make for e.g.
Led1 on if distance sensing between 10 and 20 cm
Led2 on if 20 and 50 cm
Led3 ......
tried with switch (case) and multiplue ifs always fail, what is the best way to achive this functionality?
I have also some general question about some book / tutorial on Java for total newbie before making some serious code.
Thanks in advance for help.
BR
Max
Beta Was this translation helpful? Give feedback.
All reactions