• Hi, I programmed my MDBT42Q module to make a small beep sound whenever I press or release a button. This is the code for my piezo sound effects:

    function beep(f) { 
      if (f===0) digitalWrite(spk,0);
      else analogWrite(spk, 0.5, { freq: f } );
    }
    
    function buttonBoop(f) {
      beep(f);
      setTimeout(function() {digitalWrite(spk, 0);}, 50);
    }
    

    pretty simple, right?

    So I tried to put them into setWatch functions so that my wristwatch(the PCB that houses the MDBT42Q module) can make a small beep whenever a button is pressed. The two setwatches are for when the button is pressed and released. When the button is pressed, the watch displays the hour on its two 7-seg LEDs for as long as the button is held down. when the button is released, the watch displays the minutes for 300 milliseconds. Here is the code:

    setWatch(function(e) {
      data();  // grabs date & time
      buttonBoop(650); // supposed to beep piezo at 650hz for 50 milliseconds
      while (digitalRead(select) != 1) { // the watch displays the hour
        display(1, hourFirstDigit);
        display(2, hourSecondDigit);
      }
      allClear(); // resets 7-segment displays
    }, select, {repeat: true, debounce: 50, edge: "falling"});
    
    setWatch(function(e) {
      data(); // grabs date & time
      buttonBoop(950); // supposed to beep piezo at 950 hz for 50 milliseconds
      for (i = 0; i < 30; i++) { // watch displays the minutes
        display(1, minFirstDigit);
        display(2, minSecondDigit);
      }
        allClear(); // resets the 7 seg LEDs
    }, select, {repeat: true, debounce: 50, edge: "rising"});
    }
    

    the problem is, whenever I press/release the button, the piezo speaker beeps until the LEDs are cleared. That is, when the button is pressed, the piezo beeps at 650hz until I release it, and when the button is released, the piezo beeps at 950mhz for 300mhz - which is when the LEDs are cleared. Sorry if this sounds confusing for you, I can't find a way to word it well.

    I only want the piezo to beep for 50 milliseconds, what is making it go for the entire setWatch() duration?

    The strange thing is, when do a clearWatch() and run this instead...

    setWatch(function(e) {
      buttonBoop(650);
    }, select, {repeat: true, debounce: 50, edge: "falling"});
    
    setWatch(function(e) {
      buttonBoop(950);
    }, select, {repeat: true, debounce: 50, edge: "rising"});
    

    the piezo behaves just fine. A short beep when the button is pressed, and another short beep when the button is released. It's the same setWatch() code but with all the LED/time related code removed. But I can't find anything with my LED/time code. Thought this would help.

    This is the entire source code for my watch. The buggy part is near the end.

    Thanks.

  • Sun 2019.07.07

    Good Morning @barry_b_benson,

    Agreeing with what the other have stated, but pay attention to the point made about single threaded above.

    If I understand correctly, I think this is what you are after:

    In place of Line #3

    setTimeout(function () {  buttonBoop(650);  }, 50);
    

    https://www.espruino.com/Reference#l__glĀ­obal_setTimeout



    This is untested BTW:


    Understand that the single threaded nature of Javascript will allow the buzzer to sound until it times out, before the LEDs will be allowed to illuminate. But as the duration is only 50msec, I doubt any will notice.

    EDIT: The more I think about it;

    buttonBoop(650);
    
    setTimeout(function () {  
    
      data();
    
      while (digitalRead(select) != 1) {
        display(1, hourFirstDigit);
        display(2, hourSecondDigit);
      }
    
      allClear();
    
    }, 50);
    
    

    Inside the setWatch() sound the buzzer, then after a short interval, run the rest of the desired events.



    EDIT: (much later) See #7 below

About