The code presented on this page can be downloaded or found in the Arduino examples menu under Examples → SpinWearables → Compass → Calibrated_Compass
.
#include "SpinWearables.h"
using namespace SpinWearables;
void setup() {
// Ensure all of the SpinWheel hardware is on.
.begin();
SpinWheel}
float minx, miny, minz, maxx, maxy, maxz;
void loop() {
// Read all sensor data.
.readIMU();
SpinWheel
// Measure the magnetic field along the three axes of the sensor.
float x = SpinWheel.mx;
float y = SpinWheel.my;
float z = SpinWheel.mz;
if (x > maxx) maxx=x;
if (y > maxy) maxy=y;
if (z > maxz) maxz=z;
if (x < minx) minx=x;
if (y < miny) miny=y;
if (z < minz) minz=z;
float truex = x - (maxx+minx)/2;
float truey = y - (maxy+miny)/2;
float truez = z - (maxz+minz)/2;
// Use this line if you want to skip the correction.
//uint8_t angle = (-atan2(y, x)+3.1415/2)/2/3.1415*255;
uint8_t angle = (-atan2(truey, truex)+3.1415/2)/2/3.1415*255;
.setSmallLEDsPointer(angle, 800, 0xffffff);
SpinWheel
.drawFrame();
SpinWheel}