• Hi,

    Finally I have bought some hardware to my espruino boards and I have a question:

    I started with code from distance sensing robot:

    var TRIG = A0; // ultrasonic trigger
    var ECHO = A1; // ultrasonic echo signal
    var MOTORS = [A3,A2,B10,B11]; // pins for the motors
    
    var t1 = 0;
    var dist = 0;
    var inManouver = false;
    
    // Get the distance from the sensor
    setWatch(function(e) { t1=e.time; }, ECHO, { repeat:true, edge:'rising'  });
    setWatch(function(e) { var dt=e.time-t1; dist = (dt*1000000)/57.0; },  ECHO, { repeat:true, edge:'falling' });
    // 20 times a second, trigger the distance sensor
    setInterval("digitalPulse(TRIG,1, 10/1000.0)",50);
    

    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

  • 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...

  • 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...

  • My thought would be

    setWatch(function(e) { var dt=e.time-t1; dist = dt*1000000/57.0; updateLed();},  ECHO, { repeat:true, edge:'falling' });
    
    
    function updateLed() {
    
    if (dist<10)  { //those are cm right? I've never used my HC-SR04's...
    digitalWrite(LED1,1);
    digitalWrite(LED2,0);
    digitalWrite(LED3,0);
    } else if (dist<20) {
    digitalWrite(LED1,0);
    digitalWrite(LED2,1);
    digitalWrite(LED3,0);
    } else {
    digitalWrite(LED1,0);
    digitalWrite(LED2,0);
    digitalWrite(LED3,1);
    }
    }
    

    Maybe cleaner:

    var ledpins=[LED1,LED2,LED3]; //declare with the globals
    
    //instead of the three digital writes, you can do:
    digitalWrite(ledpins,[1,0,0]); 
    
    

    (code is untested)
    Edit: Code block that already, damnit!

    (that's assuming you've checked that the dist values you're getting are sensible)

  • Thanks a lot DrAzzy with

    setInterval(updateLed,100);
    

    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.

  • 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.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Beginner playing with HC-SR04 / General how to start question

Posted by Avatar for Kaermorhen @Kaermorhen

Actions