The code is:

```
var Ruuvitag = require("Ruuvitag");
Ruuvitag.setAccelOn(true);
function totalAcceleration(accelerationX,accelerationY,accelerationZ) {
if (accelerationX === null || accelerationY === null || accelerationZ === null) {
return null;
}
return Math.sqrt(accelerationX * accelerationX + accelerationY * accelerationY + accelerationZ * accelerationZ);
}
function angleBetweenVectorComponentAndAxis(vectorComponent, vectorLength) {
if (vectorComponent === null || vectorLength === null || vectorLength === 0) {
return null;
}
var angle = Math.acos(vectorComponent / vectorLength);
//return Math.toDegrees(angle);
return toDegree(angle);
}
function toDegree(radians){
var pi = Math.PI;
return radians * (180/pi);
}
setInterval(function() {
accValues = Ruuvitag.getAccelData();
accelerationX = accValues.x;
accelerationY = accValues.y;
accelerationZ = accValues.z;
accelerationTotal = totalAcceleration(accelerationX,accelerationY,accelerationZ);
accelerationAngleFromX = angleBetweenVectorComponentAndAxis(accelerationX,accelerationTotal);
accelerationAngleFromY = angleBetweenVectorComponentAndAxis(accelerationY,accelerationTotal);
accelerationAngleFromZ = angleBetweenVectorComponentAndAxis(accelerationZ,accelerationTotal);
pitch = (((Math.atan2(accelerationAngleFromZ,accelerationAngleFromX) * 180) / 3.14 ) + 180 );
console.log("Pitch: " + pitch);
}, 500);
```

Here are the images of the axes with respect to the sensor:

At this point I made my measurements and I got (angle in degree):

Standing on the table, FaceUp to the ceiling: 180 - 182 / 358-359

FaceUp to me: 92 - 93 / 263 - 264

FaceUp down: 2 - 5 / 173 -174

FaceUp outwards: 271 / 88

Moreover, having the FaceUp towards the ceiling and tilting it towards me, the angle should be 135° in reality the one measured is 225. In fact, the values that should be between 180 and 90 but those measured are 180 and 270.

This should be because of how the sensor feels the gravity. What I need is a unique angle value following the counterclockwise fencing (see this picture).

I hope the problem is now clearer. Thank you