Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Capture wrist movement with accelerometer

My app's goal is to ping the user every 5 minutes: if the user roll their wrist, then I am sure the user is awake and if they miss to roll wrist for 3 times, I'm sure they are asleep.
Here is what I do:

  1. send a vibration -> I can achieve this using setInterval and vibration.start()
  2. capture the wrist roll through accelerometer. -> I don't get how to capture the movement, assuming the user rolled their wrist right after the vibration.

Below is the code I drafted:

// console.log("App code started");
import { vibration } from "haptics";
import * as document from "document";
import sleep from "sleep";
import * as messaging from "messaging";
import * as fs from "fs";
import { Gyroscope } from "gyroscope";
import { Accelerometer } from "accelerometer";
import { me as appbit } from "appbit";
import * as scientific from "scientific";

var minInterval = 5*60;
var noResponse = 0;

var ping = false;
var observe = false;
var responded = false;


// prevents the app to timeout due to lack of activity
appbit.appTimeoutEnabled = false;

// instantiate sleep state observer
if (sleep) {
  sleep.addEventListener("change", () => {
     console.log(`User sleep state is: ${sleep.state}`);
   });
} else {
   console.warn("Sleep API not supported on this device, or no permission")
}


// open messaging pipe to companion
messaging.peerSocket.addEventListener("open", (evt) => {
  console.log("Ready to send messages");
});
 

// verify if device has accelerometer, then start it
if (Accelerometer) {
   console.log("This device has an Accelerometer!");
    
   // add event listener to accelerometer
  var accelerometer = new Accelerometer({ frequency: 10, batch:1});  
  accelerometer.addEventListener("reading", () => {
    console.log(`ACC reading ${scientific.sum(scientific.diff(accelerometer.readings.x))}, ${scientific.sum(scientific.diff(accelerometer.readings.y))}, ${scientific.sum(scientific.diff(accelerometer.readings.z))}`);
  });
   accelerometer.start();
} else {
   console.log("This device does NOT have an Accelerometer!");}

var pingInterval = setInterval(pingUser, 5*60*1000);

function pingUser(){;  
  vibration.start("nudge");  

}

The main difficulty for me is to identify how to capture the specific movement: should I add reading frequency and batch? Where do I put this logic? In the pingUser function, eventlistener or should I create another setInterval that goes after the pingUser?

Best Answer
0 Votes
2 REPLIES 2

The accelerometer reading event is probably the best place to capture the movement. We don't provide examples of capturing specific movements, but a quick Google should reveal the type of math and logic involved for detecting gestures with an accelerometer.

Best Answer
0 Votes

I actually managed to do it. I think I started from your example of the three balls. The hardest part was to set the right moment to get the readings. Thanks!!

Best Answer