Puck.js motion tiggered music player

Posted on
  • Hi.
    I have been trying to work on a small project using Puck.js. I am not a JS expert though and thought maybe I could get some help here.
    The goal is for Puck to be a music controller similar to https://www.espruino.com/BLE+Music+Contr­ol
    but instead of clicking, significant motion (sensed by accelerometer or magnetometer) would be the trigger of play/pause/skip of songs. I know how motion detection works with Puck, but I have no clue how to combine it with being a trigger.
    Thank you in advance!

  • Fri 2021.06.11

    Hi @user129965

    'but I have no clue how to combine it with being a trigger.'

    One method might be to wire a GPIO pin used as an output, to a GPIO input pin and supply the logic for a setWatch that detects the logic level change. Inside the (say magnetomer) on() event, use digitalWrite() to set the GPIO output pin. The watch logic would respond to that logic level change on the input pin.


    See snippet for on() event and possible GPIO pins to use


  • Hi! It sounds like you figured out the motion detection bit with https://www.espruino.com/Puck.js#on-boar­d-peripherals ?

    So literally all you need to do is mash the two together. For instance using 'Movement detection' from the Puck.js page, and the code from https://www.espruino.com/BLE+Music+Contr­ol:

    var controls = require("ble_hid_controls");
    NRF.setServices(undefined, { hid : controls.report });
    var idleTimeout;
    Puck.on('accel',function(a) {
      if (idleTimeout) clearTimeout(idleTimeout);
      else {
        print("Motion", a);
        controls.next(); // <---------------- HERE
      idleTimeout = setTimeout(function() {
        idleTimeout = undefined;

    The only trick is making sure you don't call the function too often - so above I only call it the first time it turns the LED on, so you have to wait until the light goes out before you can skip to the next song.

    I guess you could look at acc.z > 0 or something like that so that depending on which way up you help the puck, it played/stopped or skipped song

  • Thank you! I have managed to get this to work using magnetometer, modifying it's sensitivity to movement so that it performs different actions, eg: small move - play/pause, big move - next song. Easier than I thought, especially given the base already existing on the forum and the guidance for Puck. Thanks once again :)

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

Puck.js motion tiggered music player

Posted by Avatar for user129965 @user129965