• Hi,

    I am new in programming via Javascript. I have a Puck.JS and a MDBT42Q and I wanna do a connection with Bluetooth and the following function:

    If both devices powered up they should connect to each other automatically. Then the simple function, when I do a click with the Puck I will switch a digital output to high and turn on the led for maybe 0,5sec at MDBT42Q and on the Puck.
    Is there also a possibility to detect double clicks?

    I tried the tutorial with the servo/flag but the pairing starts when I push the button right? Thats why there is a delay for the incoming signal on the MDBT42Q.

    Can you help me? I am a person for learning by doing but I got not forward here.


  • Wed 2020.02.05

    'I tried the tutorial with the servo/flag'


    Original tutorial


    'I am a person for learning by doing'

    Ahhh, @user108865 a "Do, Think, Do" individual. . . .

    Here are some "Think, Do, Think" examples . . . .


    Creating a half second delay with setTimeout()


    Code snippets for detecting a double click

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

  • Hi!

    @Robin put up a good link for detecting double-clicks. While it's not that hard, it's trivial to detect long vs short clicks, so you could always just do that?

    I reckon the bluetooth flag tutorial you found is a good place to start. The initial example (using UART) uses ble_simple_uart - which abstracts away so much that you can't connect separately.

    However you could just use the ble_uart module referenced at https://www.espruino.com/BLE+UART

    So the receiver stays exactly as-is on http://www.espruino.com/BLE+Communicatio­ns#receiver-bluetooth-peripheral-device-­

    Then for the transmitter

    var log = print;// or function(){} to avoid printing anything
    var uart;
    function connect() {
      log("Searching for device...");
      NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }] }).then(function(device) {
        device.on('gattserverdisconnected', function() {
          uart = undefined;
        return require("ble_uart").connect(device);
      }).then(function(u) {
        uart = u;
        uart.write("\x03"); // send ctrl-c to clear anything on the input line
      }).catch(function(e) {
        log("Error: "+e);
    function flag(e) {
      var l = e.time-e.lastTime;
      log("Button press length",l,(l>0.3)?"long":"short");
      log("Sending command");
      if (!uart) {
        log("Not connected!");
    // When button pressed
    setWatch(flag, BTN, {repeat:true, edge:"falling"});
    // start connecting...

    The page with the flag example had another option too (using custom characteristics): http://www.espruino.com/BLE+Communicatio­ns#sending-commands-custom-service-

    Working with that is basically the same - you just use device.gatt.connect() rather than require("ble_uart").connect(device)

  • Ok thank you I got it.

    One thing is strange at the MDBT42Q.
    After around 5-6 clicks the LED does not turn on anymore but the function at the PUCK is ok.
    If bluetooth is disconnected the LED1 (red) is on if I press the BTN. But bluetooth seems good, LED2 is on if I press BTN.

    Any ideas?

  • Ahh... Try changing to uart.write("\x10digitalPulse(LED,1,10);\­n");

    I guess you have the original firmware on the MDBT42Q? There was an issue where if Espruino tried to print data over BLE but nothing was reading it (as could be the case here?) it'd get stuck. It got fixed some time ago but was just after the 2v04 release, so only 2v05 and later (which haven't been released yet) will have it

  • Hi Gordon,

    with the code above it works without your modification in your last post. I think my code I used before had an issue. I will try to find out.

    Thank you very much!

  • Hi Gordon,

    the program works great. I use also a force sensing resistor, I want to buit into the Puck.js.
    The only thing is that I need to modify the program because when I remove the Battery from the Puck I get no more connection to the MDBT42Q. When I remove the power from the MDBT42Q it reconnects again.

    The the outputs (Jack connectors), I designed a PCB with the same size of the MDBT42Q and I will connect it like a sandwich furthermore with another PCB (same size) with the same battery + socket from the Puck as power supply.

    If this works it would be a great product. Regarding CE-Certification on Puck and MDBT42Q its much easier to certify as a medical product because of these references.

    It could be a great benefit for many users to have a wireless button.

    Thank you again for the informations.

    Best regards

  • Great! Glad it's working well for you.

    Removing the battery and re-applying will lose code if you had it just saved to RAM. All you need to do is move to saving to flash - check out http://www.espruino.com/Saving for an example.

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

Puck.JS connection to MDBT42Q and simple click function

Posted by Avatar for Blackhorse @Blackhorse