## Tilt Sensor (Part 6)

Part 123456789

Unless you plan to use the tilt sensor for detecting 90° tilt angles (e.g. dice position), you must calibrate the sensor prior to running accurate tilt angle measurements. There a many ways of achieving this task. The described method is pretty simple and gives very good results. Under these calibration conditions, starting at 0:0 degrees, putting a piece of paper under one of the accelerometer evaluation board reads a 1° tilt angle!

```double X_NO_G;
double X_ONE_G;
double Y_NO_G;
double Y_ONE_G;
double Z_NO_G;
double Z_ONE_G;
LCD.printString(MSG_TLT_NONE, 1);
LCD.clearLine(2);
delay(3000);
LCD.printString(MSG_COMPUTING, 2);
delay(500);
LCD.printString(MSG_DONE, 2);
delay(1000);
LCD.printString(MSG_TLT_X, 1);
LCD.clearLine(2);
delay(3000);
LCD.printString(MSG_COMPUTING, 2);
delay(500);
LCD.printString(MSG_DONE, 2);
delay(1000);
LCD.printString(MSG_TLT_Y, 1);
LCD.clearLine(2);
delay(3000);
LCD.printString(MSG_COMPUTING, 2);
delay(500);
Z_NO_G /= 2; /* Compute average */
LCD.printString(MSG_DONE, 2);
delay(1000);
/* Compute sensitivities */
vSensitivity[channelX] = (X_ONE_G - X_NO_G);
vSensitivity[channelY] = (Y_ONE_G - Y_NO_G);
vSensitivity[channelZ] = (Z_ONE_G - Z_NO_G);
/* Compute offsets */
vOffset[channelX] = X_NO_G;
vOffset[channelY] = Y_NO_G;
vOffset[channelZ] = Z_NO_G;```

This code must be placed on top of the sketch, in the setup() function. At boot, the user is prompted to leave the sensor horizontal; after a short while, he is prompted to tilt it along the X axis, and after an other short while, he is prompted to tilt it along the Y axis.

Here is the declaration of the constants

```#define MSG_CALIBRATING "CALIBRATING..."
#define MSG_COMPUTING "COMPUTING..."
#define MSG_DONE "DONE"
#define MSG_FIRMWARE "**  MicroTLT  **" #define MSG_TLT_NONE "NO TILT"
#define MSG_TLT_X "TILT IN X DIR."
#define MSG_TLT_Y "TILT IN Y DIR."