// === ENTER NRF.getAdress() results of the couple of // === puck.js devices you want to identify each other const his = "12:34:56:78:9a:bc", hers = "fe:dc:ba:98:76:54"; let me = NRF.getAddress(), other = me===hers ? his : hers; let other_id = `${other} random`; // heart-beat LED animation sequence let seq = [1,1,0,1,1,0,0,0,0], seq_idx = 0; let anim_interval_id = 0, main_interval_id = 0; let active = false, grace_period = 0; function anim_step() { // a single heart-beat animation step digitalWrite(LED1, seq[seq_idx]); seq_idx = (seq_idx+1)%seq.length; } // set interval (1/rate) of animation. 0 means stop animation function animate(interval) { if (anim_interval_id) { clearInterval(anim_interval_id); digitalWrite(LED1, 0); anim_interval_id = 0; } if (interval) { anim_interval_id = setInterval(anim_step, interval); } } let main_event; main_event = function() { if (grace_period) { grace_period--; if (!grace_period) { if (active) { active = false; if (main_interval_id) { clearInterval(main_interval_id); main_interval_id = 0; } console.log("Deactivated"); } else { active = true; console.log("Activated"); } animate(0); } return; } NRF.requestDevice( {filters: [{id: other_id}]} ).then( function(device) { let rssi = device.rssi, interval = Math.max(30, 120-3*(rssi+80)); console.log(rssi, interval); animate(interval); }, function(e) { console.log(e); animate(0); } ); }; // Button activates/deactivates // Also animates for a grace period: // Slow for activate, fast for deactivate setWatch(function() { grace_period = 2; if (active) { console.log("Deactivating"); animate(20); } else { console.log("Activating"); animate(100); if (!main_interval_id) { main_interval_id = setInterval(main_event, 3000); } } }, BTN, {edge:"rising", debounce:50, repeat:true});