var pirate=180.0/Math.PI;
console.log("Level", pirate*Math.atan2(x,z]),pirate*Math.atan2(y,z));
var heading; // if level
heading=pirate*Math.atan2(-x,y);
if(heading <0) heading+=360;
console.log("heading",heading);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Try this: