• hi anyone tried to deal with this? I mean that could help to control few functions just by one button,
    I've seen some tutorials for Arduino, is anything for Espruino as well?

  • @allObjects made some code to handle this - there's a post on it here: http://forum.espruino.com/conversations/­1781/

    It might be quite fun to do it yourself though. Using setWatch it's pretty easy to measure the time between button presses and to set/cancel timeouts.

  • And doing short and long press is very easy:

    setWatch(function(e){
      var isLong = (e.time-e.lastTime)>0.4;
      // ...
    }, BTN, {repeat:true, debounce:50, edge:"falling"});
    
  • Hi, I was trying to set led to turn it on or off depends of how long I press the button, I used your code above.
    my code

    setWatch(function(e){
        var isLong = (e.time-e.lastTime)>0.4;
        print(isLong.toString());
        onD23();
          // ...
        }, BTN, {repeat:true, debounce:50, edge:"falling"});
    
    setWatch(function(e){
        var isLong = (e.time-e.lastTime)>0.4;
        print(isLong.toString());
        offD23();
          // ...
        }, BTN, {repeat:false, debounce:50, edge:"falling"});
    
    
    function onD23(){
      digitalWrite(D23,1);
      setTimeout(function () { digitalWrite(D23,0); }, 1000);
    }
    
    function offD23(){
      digitalWrite(D23,0);
      setTimeout(function () { digitalWrite(D23,1); }, 1000);
    }
    

    but I have same sequence on the board, when press the button my LED(D23) is ON for a 1000ms and then off, so when I press button short or long time not change nothing

  • Unless I'm totally misunderstanding, you need to actually check the value in theisLong variable and do something with it, rather than having 2 watches:

    setWatch(function(e){
        var isLong = (e.time-e.lastTime)>0.4;
        print(isLong.toString());
        if (isLong) onD23();
        else offD23();
      }, BTN, {repeat:true, debounce:50, edge:"falling"});
    
    function onD23(){
      console.log("onD23");
      digitalWrite(D23,1);
      setTimeout(function () { digitalWrite(D23,0); }, 1000);
    }
    function offD23(){
      console.log("offD23");
      digitalWrite(D23,0);
      setTimeout(function () { digitalWrite(D23,1); }, 1000);
    }
    

    It's still going to get a bit confusing though - it'd be easier to check if you had 2 different LEDs.

  • cool, thank you, it is working now ;)
    I wanted to control different functions just by one button by short and long presses,
    I need a time to understand construction of programming language you use for espruino,
    now sometimes it remain me arduino way and sometimes python,
    should I learn more python to understand espruino programming way better? or in espruino are some special ways to use some functions which are not simply same as those functions wrote in strict python?

  • Great!

    Espruino is actually JavaScript, not Python. The thing that you will find strange is the 'event-based' nature of JavaScript that is different to most C code. It does have its advantages though!

    Espruino's JavaScript is the same as 'strict' JavaScript - it is missing one or two features that you might find in desktop JavaScript (like regular expressions), but there are no language features specific to Espruino.

    There are one or two extra functions in Espruino though - like setWatch, digitalWrite, etc - that aren't available on the desktop because your computer doesn't have GPIO pins on it!

    Learning some JavaScript might help, but I think you would find it easiest just to experiment on Espruino itself. It's really quite easy once you get over the initial hurdles.

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

One or double click the button and time duration for long or short press?

Posted by Avatar for bigplik @bigplik

Actions