You are reading a single comment by @Robin and its replies. Click here to read the full conversation.
  • Thanks a lot for pointing me in the right direction. I lacked the knowledge to realize that I don't need a second Bangle.on('twist'). And now I realize my newbie question is quite senseless, because a bit more of "if...else-understanding" gets the job done easily.

    So here is the less function-cluttered "app":

    /////////////////////////////////////////////////////////////
    //   control music by twist/buttons
    
    let counter = 0; //stores your counted your twists
    var tstate = false; //are you ready to count the twists?
    
    function playx() {
      Bluetooth.println(JSON.stringify({t:"music", n:"play"}));
    }
    
    function volu() {
      Bluetooth.println(JSON.stringify({t:"music", n:"volumeup"}));
    }
    
    function vold() {
      Bluetooth.println(JSON.stringify({t:"music", n:"volumedown"}));
    }
    
    function sendCmd() {
      print (counter);
      Bangle.beep(200,3000);
      if (tstate==false && counter>0){
      do {playx(); counter--;}
      while (counter >= 1);
      }
    }
    
    function twistctrl() {
      if (tstate==false){
        tstate=true;
        setTimeout('tstate=false',4000);
        setTimeout(sendCmd,4100);
        Bangle.beep(200,3000);
      }
      else{
      if (tstate==true){
      if (counter < 5){
        counter++;
        Bangle.buzz(100,2);
        }
      else {
        counter = 0;
        Bangle.buzz(400);
           }
      }
      }
    }
    
    setWatch(volu,BTN1,{repeat:true});
    setWatch(vold,BTN3,{repeat:true});
    Bangle.on('twist',twistctrl);
    setWatch(Bangle.showLauncher, BTN2, {repeat:false,edge:"falling"});
    
  • Wed 2021.05.19

    'I realize my newbie question is quite senseless'

    On the contrary, . . . it was necessary to learn the if-else construct prior to being able to realize that the second twist event would not be required. In your mind before that eureka moment, there wasn't a means to get to that realization, so at that point, creating this post was one of the only means to get to the solution you found. Quite logical when one views this thread thinking the events in reverse.

    Using Gordon's #6 post which is a refinement of the hint I provided in #4 post, allowed you to resolve in a very short period of time the solution I (we) intended you to get to. So, @radswid in around a 24hr period, you were able to comprehend what we were communicating, even though was in more of a hint format, rationalize how that needed to fit into what sequential event sequence wasn't occurring, use intuition and maybe a bit of language syntax learning lookup, in order to present a better structured and simpler coding result, code and debug that solution and all in one evening to-boot!! Are you sure you are a beginner??    ;-)

    Now, please help me here. I'd like to create some tutorials (not related to this project) and have my own references. You indiated in post #3 two references, (the API Reference helped a ton and mozillas MDN JavaScript reference was very helpful too) are there by chance any others that you are using that helped here?

    Now that the final solution is near, there are a couple of observations I'd like to make. Please don't take this as criticism, as it usually takes quite a bit of time before patterns and coding techniques are mastered. And since this solution was found overnight, that hardly fits into
    the 'quite a bit of time' duration.

    Carefully review L36 in post #7 and review examples at MDN:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

    The hint here is to simplify. See how that task now may be an improvement.

    I have several others, that we may tackle one-at-a-time.

  • Thanks for your kind words, @Robin.

    'I realize my newbie question is quite senseless'

    I wanted to express, that I feel a bit guilty asking questions which could've been answered by myself by a revision of my code and re-reading some tutorial-parts. But maybe you're right, sometimes you need a kind of other "input" to get to a solution.

    Are you sure you are a beginner??

    Before starting this project I copypasted tutorial-codesnippets into the IDE or tried changing code-parts of e.g. a clock app to change its look. So I guess so.

    You indiated in post #3 two references, (the API Reference helped a ton and mozillas MDN JavaScript reference was very helpful too) are there by chance any others that you are using that helped here?

    I tried several Youtube-tutorials, but never found one which focuses on javascript without combining it with html and css. So the great tutorials on espruino.com and those mentioned references were the only "sources of knowledge" for me.

    Carefully review L36 in post #7 and review examples at MDN:

    Ah, it's obviously unnecessary, as there are only two conditions for tstate :D . Should've rechecked before posting.

    Thanks again for spending your time on my gain of knowledge! Really appreciate that!

About

Avatar for Robin @Robin started