Technoromantic jewelery (based on puck.js)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

proximibeat.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // === ENTER NRF.getAdress() results of the couple of
  2. // === puck.js devices you want to identify each other
  3. const his = "12:34:56:78:9a:bc", hers = "fe:dc:ba:98:76:54";
  4. let me = NRF.getAddress(), other = me===hers ? his : hers;
  5. let other_id = `${other} random`;
  6. // heart-beat LED animation sequence
  7. let seq = [1,1,0,1,1,0,0,0,0], seq_idx = 0;
  8. let anim_interval_id = 0, main_interval_id = 0;
  9. let active = false, grace_period = 0;
  10. function anim_step() { // a single heart-beat animation step
  11. digitalWrite(LED1, seq[seq_idx]);
  12. seq_idx = (seq_idx+1)%seq.length;
  13. }
  14. // set interval (1/rate) of animation. 0 means stop animation
  15. function animate(interval) {
  16. if (anim_interval_id) {
  17. clearInterval(anim_interval_id);
  18. digitalWrite(LED1, 0);
  19. anim_interval_id = 0;
  20. }
  21. if (interval) {
  22. anim_interval_id = setInterval(anim_step, interval);
  23. }
  24. }
  25. let main_event;
  26. main_event = function() {
  27. if (grace_period) {
  28. grace_period--;
  29. if (!grace_period) {
  30. if (active) {
  31. active = false;
  32. if (main_interval_id) {
  33. clearInterval(main_interval_id);
  34. main_interval_id = 0;
  35. }
  36. console.log("Deactivated");
  37. } else {
  38. active = true;
  39. console.log("Activated");
  40. }
  41. animate(0);
  42. }
  43. return;
  44. }
  45. NRF.requestDevice(
  46. {filters: [{id: other_id}]}
  47. ).then(
  48. function(device) {
  49. let rssi = device.rssi, interval = Math.max(30, 120-3*(rssi+80));
  50. console.log(rssi, interval);
  51. animate(interval);
  52. },
  53. function(e) {
  54. console.log(e);
  55. animate(0);
  56. }
  57. );
  58. };
  59. // Button activates/deactivates
  60. // Also animates for a grace period:
  61. // Slow for activate, fast for deactivate
  62. setWatch(function() {
  63. grace_period = 2;
  64. if (active) {
  65. console.log("Deactivating");
  66. animate(20);
  67. } else {
  68. console.log("Activating");
  69. animate(100);
  70. if (!main_interval_id) {
  71. main_interval_id = setInterval(main_event, 3000);
  72. }
  73. }
  74. }, BTN, {edge:"rising", debounce:50, repeat:true});